ng-init in angular不起作用

时间:2017-04-12 09:54:13

标签: javascript angularjs

我是angularjs的新手。试图用它来构建简单的应用程序,我在名为cuurent的控制器中定义了一个变量,现在我想设置输入字段的值,如下所示:

  <div class="form-group">
        <input type="text" class="form-control" name="UserName" ng-model="management.regData.name" placeholder="UserName" ng-init="management.regData.name = '{{current.FullName}}'"  required>
        <p class="help-block" ng-show="(!editForm.UserName.$pristine && editForm.UserName.$error.required) || (editForm.$submitted && editForm.UserName.$error.required) ">this field is required</P>
          <p class="help-block" ng-show="regUser.usernameMsg && !editForm.UserName.$error.required && editForm.UserName.$valid">{{ regUser.usernameMsg }}</p>
          <ul ng-show="(!editForm.UserName.$pristine && editForm.UserName.$error.pattern) || (!editForm.UserName.$pristine && editForm.UserName.$error.minlength) || (!editForm.UserName.$pristine && editForm.UserName.$error.maxlength)" class="help-block">
            <li>Must not contain any special characters or spaces</li>
            <li>Must be at least 3 characters but no more than 25</li>
          </ul>
        </div>   

问题是ng-init =“management.regData.name ='{{current.FullName}}'”不起作用。 有什么建议 ?

3 个答案:

答案 0 :(得分:2)

在没有表达式的情况下使用,请删除{{}}

 ng-init="management.regData.name = current.FullName"

答案 1 :(得分:1)

您不应在{{}}内使用绑定表达式ng-init。您还需要删除''周围的current.FullName

更改您的代码表格

ng-init="management.regData.name = '{{current.FullName}}'"

ng-init="management.regData.name = current.FullName"

答案 2 :(得分:1)

删除大括号,然后添加

ng-init="management.regData.name = current.FullName"

演示

&#13;
&#13;
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.management={"regData":{"name":""}}
$scope.current = {"FullName":"sample"}
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <input type="text" class="form-control" name="UserName" ng-model="management.regData.name" placeholder="UserName" ng-init="management.regData.name = current.FullName"  required>
</div>
&#13;
&#13;
&#13;