angularjs – 为什么格式化程序不适用于隔离范围?

前端之家收集整理的这篇文章主要介绍了angularjs – 为什么格式化程序不适用于隔离范围?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么格式化程序不适用于隔离范围?这是有角度的错误还是我做错了什么?

这包含隔离范围,不起作用:http://jsfiddle.net/YbdXQ/56/

restrict: 'A',scope:{},link: function(scope,elm,attrs,ctrl) {
      ctrl.$formatters.unshift(function(modelValue) {
          console.log("In formatters" + modelValue);
         return $filter('date')(modelValue);
     });

这不包含隔离和范围工作正常:http://jsfiddle.net/YbdXQ/57/

restrict: 'A',ctrl) {
      ctrl.$formatters.unshift(function(modelValue) {
          console.log("In formatters" + modelValue);
         return $filter('date')(modelValue);
     });

解决方法

这与格式化程序没有任何关系,而是ngModel不再能够访问您尝试传递它的值.当您创建隔离范围时,ngModel指令不再可以使用myDate(因为您已经创建了一个新范围 – 隔离范围 – 它上面没有myDate).作为证据,这里是一个 not-so-useful example,根据传入ngModel属性内容在范围上设置myDate: http://jsfiddle.net/YbdXQ/78/

angular/angular.js#1069,“一个指令的隔离范围隔离了同一元素上的其他指令”,讨论了这个问题:

For example,notice how my custom directive is preventing ng-model from working

您可能也对this StackOverflow question,“ngModel and component with isolated scope”感兴趣.

猜你在找的Angularjs相关文章