AngularJS更新ng-model的值以设置默认值
angularjs update value of ng-model to set default value
我正在尝试设置用户名($scope.userName)和密码($scope.password)的默认值,我已经尝试过使用ng-init,但它不起作用,但它不起作用这是我的代码:
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
};
}]);
这是我的 HTML
<div class="modal-body" login-user >
<form class="custom-form" name="loginForm" ng-submit="loginSubmit()" novalidate>
<label class="museo_sans500" for="email">Nome utente o indirizzo email</label>
<input type="text" class="form-control museo_sans300" id="userName" placeholder="Nome utente o indirizzo email" ng-init="userName='<?php echo "Yesss" ?>'" name="UserName" minlength="5" ng-model="userName" required="required">
<div class="form-group">
<label class="museo_sans500" for="password">Password</label>
<input type="password" class="form-control museo_sans300" id="password" placeholder="Password" name="password" minlength="5" ng-model="password" required="required">
</div>
<div class="form-group">
<label class="museo_sans700"><a href="javascript:void(0)" ng-click="forgotPasswordShow()">Hai dimenticato la password?</a></label>
<label class="museo_sans700" style="margin-left: 30px;"><input type="checkbox" ng-model="rememberMe" name="remember-me"> Ricordami</label>
</div>
<button type="submit" class="btn play-now museo_sans500 m-t-20px login-btn" ng-disabled="loginForm.$invalid">ENTRA</button>
<div class="or museo_sans500">oppure</div>
</form>
</div>
提前致谢
而不是
ng-init
您应该在controller/directive
中分配默认值,而在输入字段中只需使用 ng-model
即可。
在指令中:
$scope.userName = 'Ali'; // default name
在 HTML 中:
<input type="text" class="form-control" id="userName" placeholder="name" name="UserName" ng-model="userName" required="required">
普兰克演示
对于属性占位符的值,请使用:
<input type="password" class="form-control museo_sans300" id="password" placeholder="{{password}}" name="password" minlength="5" ng-model="password" required="required">
很确定这应该有效。如果您仍然遇到问题,请告诉我如何。
相关文章:
- jQuery Datepicker从日期开始设置默认值
- 在KeystoneJS中为Types.Money设置默认值
- 如何为显示jquery滑块值的文本框设置默认值
- 在Bootstrap下拉菜单中设置默认值
- 如何为 jquery 自动完成框设置默认值
- 聚合物:如何为具有嵌套特性的图纸选项卡设置默认值
- 使用复杂对象上的删除为选择框手动设置默认值
- 在动态输入数据的下拉菜单中设置默认值
- 有什么方法可以在超级测试中设置默认值吗
- 挖空 + 选择2 -- 设置默认值
- ||运算符在 null 时不设置默认值
- 如何在 Select2 输入中设置默认值,并在 asp.net mvc 中标记
- 在链接回以前访问过的页面时设置默认值
- PHP 设置默认值以从 ajax 页面返回记录
- 在文档加载时使用 JQuery 在下拉字段中设置默认值
- 如何为 Google 放置 API 自动完成文本框设置默认值
- 为具有未定义值的嵌套对象设置默认值的最简单方法
- AngularJS更新ng-model的值以设置默认值
- 剑道 UI 组合框 - 设置默认值
- JQuery 日期选取器设置默认值