为什么ng-change没有在angular js中启动



你能告诉我为什么ng-change没有在angular js中启动吗?当我在日期输入字段中键入"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=preview步骤以再现此错误

  1. 运行应用程序。第一个日期字段是空的。我刚刚在里面输入了1,然后ng被激发了,然后我键入了ant,它不被激发

因为当您键入a时,模型值将变为undefined,当您键入b时,模型价值未定义,因此没有任何变化,当您输入c时,模型将再次变为undefined。这就是为什么CCD_ 7没有被调用。

来自angularjs文档:

ngChange表达式仅在输入发生更改时求值值导致将新值提交给模型。

不会进行评估:

如果从$parsers转换管道返回的值如果输入自模型以来一直无效,则不更改如果以编程方式而不是通过更改为输入值

这里有一个助手指令,我用于这样的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() />

最新更新