从指令更改ngModel值 [英] Change the ngModel value from a directive
问题描述
我使用AngularJS和我创建的要求ngModel一个指令:
使用严格的;
angular.module('spot.im.embed')。指令(sayBox',['$ SCE,$超时','$解析,
功能(SCE $,$超时,$解析){
返回{
限制:EA,
要求:'ngModel',
范围: {
},
链接:功能(范围,iElement,iAttrs,ngModel){
。ngModel $ viewValue ='adasd';
}
}
}
]);
Fot的某些原因我不知道,NG-模型的变化不会对视图产生影响。这是为什么?这是从指令改变ngModel价值的正确方法?
$ viewValue 是财产, $ setViewValue 是方法,你可能正在寻找
链接:功能(范围,iElement,iAttrs,ngModel){
ngModel $ setViewValue('adasd');
。ngModel $渲染(); //取决于 - 如果你想更新示波器的输入值或唯一模式的价值
}
$ setViewValue(值,触发);
当输入指令要改变视图值此方法应被调用;典型地,这是从一个DOM事件处理程序中完成。
块引用>文件: https://docs.angularjs.org/api/ng/类型/ ngModel.NgModelController
I am using AngularJS and I created a directive that requires 'ngModel':
'use strict'; angular.module('spot.im.embed').directive('sayBox', ['$sce', '$timeout', '$parse', function($sce, $timeout, $parse) { return { restrict: 'EA', require: 'ngModel', scope: { }, link: function(scope, iElement, iAttrs, ngModel) { ngModel.$viewValue = 'adasd'; } } } ]);
Fot some reasons I don't know, the ng-model changes doesn't impact on the view. Why is that? Is this the right way to change the ngModel value from a directive?
解决方案$viewValue is property, $setViewValue is method that you are probably looking for
link: function (scope, iElement, iAttrs, ngModel) { ngModel.$setViewValue('adasd'); ngModel.$render(); // depends – if you want update input value or only model value in the scope }
$setViewValue(value, trigger);
This method should be called when an input directive want to change the view value; typically, this is done from within a DOM event handler.
documentation: https://docs.angularjs.org/api/ng/type/ngModel.NgModelController
这篇关于从指令更改ngModel值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!