加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 使用ng-model后缺少textarea的默认值

发布时间:2020-12-17 09:03:53 所属栏目:安全 来源:网络整理
导读:我有一个角度应用程序,我在其中使用如下文本框: div class="panel-body text-center" textarea id="mytext" class="form-control" rows="4"John,2 Jane,3 John,4 Jane,5 /textarea /div 这里默认加载值John,2 …等.但是,当我按如下方式引入ng-model访问此数
我有一个角度应用程序,我在其中使用如下文本框:
<div class="panel-body text-center">
        <textarea id="mytext" class="form-control" rows="4">John,2
    Jane,3
    John,4
    Jane,5
        </textarea>
    </div>

这里默认加载值John,2 …等.但是,当我按如下方式引入ng-model访问此数据时,默认值不再显示.可能会发生什么?

<textarea id="mytext" ng-model="mytextvalue" class="form-control" rows="4">
从 docs:

ngModel will try to bind to the property given by evaluating the expression on the current scope. If the property doesn’t already exist on this scope,it will be created implicitly and added to the scope.

那么这里发生的事情如下:

<textarea ng-model="mytextvalue" >...</texarea>

处理ng-model指令时,它将在$scope上查找属性mytextvalue.如果找不到,则会创建一个空值,然后继续将该空值分配给您的元素.

如果要默认值,则必须明确指定mytextvalue的值

您可以使用ng-init在HTML中执行此操作

ng-init="mytextvalue='John,2nJane,3nJohn,4nJane,5'"

或者您可以在控制器上使用JavaScript执行此操作:

$scope.mytextvalue = 'John,5';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读