为什么格式化程序不适用于隔离范围? [英] Why formatters does not work with isolated scope?

查看:23
本文介绍了为什么格式化程序不适用于隔离范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why formatters does not work with isolated scope? Is this angular bug or I am doing something wrong?

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

This contains isolates scope and does not work: 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/

This does not contain isolated and scope works fine: 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);
     });

推荐答案

这与格式化程序没有任何关系,而是 ngModel 不再可以访问您的值这一事实重新尝试通过它.当您创建隔离作用域时,myDate 不再可用于 ngModel 指令(因为您已经创建了一个新作用域——一个隔离作用域——上没有 myDate).作为证明,这里有一个 不太有用的例子,它设置了 myDate基于传递给 ngModel 属性的范围:http://jsfiddle.net/YbdXQ/78/

This doesn't have anything to do with formatters, but rather the fact that ngModel no longer has access to the value you're trying to pass it. When you're creating an isolate scope, myDate is no longer available to the ngModel directive (since you've created a new scope--an isolate scope--that doesn't have myDate on it). As proof, here's a not-so-useful example that set's myDate on the scope based on what's passed in to the ngModel attribute: http://jsfiddle.net/YbdXQ/78/

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

angular/angular.js#1069, "One directive's isolation scope isolates other directives on the same element," talks about this very problem:

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

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

您可能还对 这个 StackOverflow 问题ngModel 和具有独立作用域的组件"感兴趣".

这篇关于为什么格式化程序不适用于隔离范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆