1
我有一个指令,我有一个模板,我试图在用户点击输入时更新ng模型。我能够抓住事件并尝试更新价值。我有元件上的观察者的控制器,但观察者功能永远不会被触发后commitViewValue
(function() {
'use strict';
angular.module('aa')
.directive('ss', [
function() {
return {
restrict: 'EA',
replace: true,
transclude: true,
scope: {
placeholder: '@'
},
require: 'ngModel',
template: '<div class="clear">' +
'<input type="text" ng-model="ngModel" ng-model-options="{updateOn:\'change blur\'}" />' +
'</div>',
link: function (scope, elem, attrs, ngModelCtrl) {
elem.bind("keyup",function(e) {
if (e.keyCode === 13) {
e.stopImmediatePropagation();
ngModelCtrl.$commitViewValue();
scope.$apply(ngModelCtrl.$setTouched);
}
});
}
}
}
]);
})();
$scope.$watch('pr', function(newValue,oldValue){
console.log('val>>' + newValue);
});
<ss ng-model="pr"></ss>
感谢您的回答!完美的作品。 – phantomsays