代码:http ://plnkr.co/edit/xPZM5E7tjYqlt5NIabIu?p=preview line-no:17
在这段代码中,如果我使用ctrl.$modelValue = nVal;
而不是 $parse(attrs.ngModel).assign(scope, nVal);
then 它不起作用。你能指出原因吗?
angModule.directive('moChangeProxy', function ($parse) {
return {
require:'^ngModel',
restrict:'A',
link:function (scope, elm, attrs, ctrl) {
var proxyExp = attrs.moChangeProxy;
scope.$watch(proxyExp, function (nVal) {
if (nVal != ctrl.$modelValue) {
//ctrl.$modelValue = nVal; // This does not work
$parse(attrs.ngModel).assign(scope, nVal); // This works well
}
});
elm.bind('blur', function () {
var proxyVal = scope.$eval(proxyExp);
if(ctrl.$modelValue != proxyVal) {
scope.$apply(function(){
$parse(proxyExp).assign(scope, ctrl.$modelValue);
});
}
});
}
};
});