【问题标题】:Why formatters does not work with isolated scope?为什么格式化程序不适用于隔离范围?
【发布时间】:2013-01-07 21:16:46
【问题描述】:

为什么格式化程序不适用于隔离范围?这是角度错误还是我做错了什么?

这包含隔离范围并且不起作用: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',
 link: function(scope, elm, attrs, ctrl) {
      ctrl.$formatters.unshift(function(modelValue) {
          console.log("In formatters" + modelValue);
         return $filter('date')(modelValue);
     });

【问题讨论】:

    标签: angularjs angularjs-scope


    【解决方案1】:

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

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

    例如,注意我的自定义指令是如何阻止 ng-model 工作的

    您可能还对this StackOverflow question, "ngModel and component with isolated scope"感兴趣。

    【讨论】:

      猜你喜欢
      • 2014-10-07
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多