angularjs更新ng-model的值以设置默认值

时间:2016-02-09 10:57:57

标签: javascript angularjs

我正在尝试设置用户名($ scope.userName)和密码($ scope.password)的默认值,我尝试过使用ng-init但是不工作,但它不在这里是我的代码:

<div>
   <title>Title 1</title>
   <p>Paragraph one</p>
</div>
<div>
   <title>Title 1</title>
   <p>Paragraph eleven</p>
</div>

这是我的HTML

APP.directive('loginUser', ['$http','$interval','$rootScope','$timeout', function ($http,$interval,$rootScope,$timeout) {
function link($scope, element, attrs) {

    $scope.userName = 'Ali';
    $scope.password = 'pass';

}

return {
    link: link
};


}]);

提前致谢

2 个答案:

答案 0 :(得分:1)

而不是ng-init您应该在controller/directive中指定默认值,而在输入字段中只需使用ng-model

指令:

$scope.userName = 'Ali'; // default name

in html:

<input type="text" class="form-control" id="userName" placeholder="name" name="UserName" ng-model="userName" required="required">

PLUNKER DEMO

答案 1 :(得分:1)

对于属性占位符的值,使用:

<input type="password" class="form-control museo_sans300" id="password" placeholder="{{password}}" name="password"  minlength="5" ng-model="password"  required="required">

很确定这应该有效。如果你继续遇到问题,请告诉我。