[英]why ng-change not fired in angular js?
你能告訴我為什么在Angular JS中不觸發ng-change嗎? 當我在日期輸入字段中鍵入“ abc”時,它僅觸發一次。 請告訴我如何多次觸發,這是我的代碼
$scope.changedate =function(){
console.log('asda')
}
<li ng-repeat="(key, x) in c">
<p class="input-group" ng-if="x.date=='date'">
<input type="text" ng-change='changedate()' class="form-control" uib-datepicker-popup="{{format}}" ng-model="x.name" is-open="x.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="formats" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open1(x)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
<input id='{{key}}' ng-if="x.date!='date'" type="text" ng-model='x.name' value="{{x.name=='abc'?'ddd':'hhh'}}"/>
</li>
http://plnkr.co/edit/neixp9ZARRAQ33gKSV9u?p=預覽tep重現此錯誤
1
,然后ng改變了觸發狀態,然后再次鍵入不觸發的內容 因為當您鍵入a
,模型值變為undefined
,當您鍵入b
,模型值未定義,因此沒有任何變化,因此當您鍵入c
,模型又undefined
。 這就是為什么不調用ng-change
原因。
從angularjs文檔中:
僅當輸入值的更改導致將新值提交給模型時才評估ngChange表達式。
不會被評估:
如果從$ parsers轉換管道返回的值未更改,如果輸入繼續無效,因為如果通過編程方式更改模型而不是不更改輸入值,則模型將保持為null
這是我用於此類Senario的幫助程序指令:
angular.module('viewValueChanged', [])
.directive('viewValueChanged', viewValueChangedDirective);
function viewValueChangedDirective() {
return {
restrict: "A",
require: 'ngModel',
link: linkFn
};
function linkFn(scope, elem, attrs, ngModel) {
scope.$watch(function () {
return ngModel.$viewValue;
}, function (newValue, oldValue) {
if (newValue && newValue !== oldValue) {
scope.$parent.$eval(attrs['viewValueChanged']);
}
// in case of user entered invalid value
if(newValue === null) {
scope.$parent.$eval(attrs['viewValueChanged']);
}
});
}
}
並像這樣使用它:
<input uib-datepicker-popup view-value-changed="vm.onFieldsChange() />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.