如何在控制器中获取输入的ngModel
How to get ngModel of input in the controller?
考虑我有控制器和ngModel
输入。
我想在控制器中获得这个ngModel,并在它上执行$setViewValue
。
该片段已附加。尝试执行-$setViewValue
时出现错误-
TypeError: Cannot set property '$setViewValue' of undefined
var myAppModule = angular.module('myApp', []);
myAppModule.controller('myCtrl',function ($scope) {
$scope.entityMail.$setViewValue("blabla@aaa.com");
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
<div ng-app="myApp">
<form ng-controller="myCtrl">
Mail <input type="text" ng-model="entityMail">
</form>
</div>
如何正确获取该ngModel并在其上执行$setViewValue
?
编辑:
我编辑了代码。我必须和$setViewValue
一起使用。
var myAppModule = angular.module('myApp', []);
myAppModule.controller('myCtrl',function ($scope,$interval) {
$scope.entityMail = "blabla@aaa.com";
});
首先必须将$scope.entityMail声明为对象,然后只有您才能在其中应用$setViewValue。
var myAppModule = angular.module('myApp', []);
myAppModule.controller('myCtrl',function ($scope) {
$scope.entityMail = {};
$scope.entityMail.$setViewValue("blabla@aaa.com");
});
您可以直接将值分配给ngModel ,而不是$setViewValue
$scope.entityMail = "blabla@aaa.com";
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 名称输入的索引
- 如何编写HTML输入的JS内联
- 要求输入在数据列表中
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 如何将输入(type=text)从html表单传递到javascript函数
- 单击jquery清除输入值
- AngularJS-将数组值绑定到输入ngModel
- 为什么我的AngularJS ngModel绑定到时间输入,而不绑定到日期输入
- 如何在控制器中获取输入的ngModel
- AngularJs ngModel 值在使用 IME 日语输入数字时重复Microsoft
- 输入具有值,但 ngModel 为空
- 在指令中的输入上使用ngModel,保持ng-*兼容性
- 使用ngModel的自定义输入指令不会更新模型值
- 具有独立作用域的指令中的表单输入:如何将ngModel设置为正确的作用域
- 如何在AngularJS中设置输入的值,而不影响其ngModel
- 在Angular2/Typescript中不使用ngModel访问输入字段值
- 使用 AngularJS ngModel 和表单验证来$watch两个元素,并在任一输入更改时测试它们是否相等