我最近了解了ngModelController,并一直在试验它。我的指令是一个输入字段,在其keydown上,我将模型的值设置为ngModelController。$ setViewValue。它应该调用angular documentation中写的$ render并在此blog中进行解释。但是下面的代码中的$ render不会被调用。请建议。
angular.module('controls',[]);
angular.module('controls')
.controller('Spinner',['$scope',
function($scope){
$scope.spinner="";
}
])
.directive('spinControl',function(){
return {
restrict : 'E',
templateUrl : 'spin.html',
require : 'ngModel',
link : function(scope,elem,attrs,ngModelController){
ngModelController.$render = function() {
alert('render called');
};
elem.on('keyup', function(event){
ngModelController.$setViewValue(elem.find('input').val());
});
}
};
});
P.S。 - 我试图在SO上找到类似的问题,但无法找到它。指导我的问题是否重复。
答案 0 :(得分:1)
$render
。 $setViewValue
不会触发它(因为它假设,因为您的观点发生了变化,所以已经呈现了所需的更改)。
如果您有<spin-control ng-model="spinner"></spin-control>
,请尝试将其放入Spinner控制器:
$timeout(function () {
$scope.spinner = 'new value';
}, 1000);
(记得将$timeout
注入您的控制器)。加载控制器1000秒后,alert()
应显示出来。