为什么格式化并不孤立范围工作? [英] Why formatters does not work with isolated scope?

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

问题描述

为什么格式化并不孤立范围工作?难道这角错误或我做错了什么?

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

 限制:'A',
 范围:{},
 链接:功能(范围,榆树,ATTRS,CTRL){
      Ctrl键。$ formatters.unshift(功能(modelValue){
          的console.log(在格式化+ modelValue);
         返回$过滤器('日期')(modelValue);
     });

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

 限制:'A',
 链接:功能(范围,榆树,ATTRS,CTRL){
      Ctrl键。$ formatters.unshift(功能(modelValue){
          的console.log(在格式化+ modelValue);
         返回$过滤器('日期')(modelValue);
     });


解决方案

这并没有什么关系格式化,而是一个事实,即 ngModel 不再有访问值你想通过它。当你创建一个隔离范围,数值指明MyDate 不再提供给 ngModel 指令(因为你已经创建一个新的作用域 - 一个隔离范围 - 即不具有数值指明MyDate 就可以了)。为了证明这一点,这里有一个不那么有用的例子该组的数值指明MyDate 基于什么在传递到 ngModel 属性范围:的http://的jsfiddle .NET / YbdXQ / 78 /

角/ angular.js#1069 ,一个指令的隔离范围隔离在相同的其他指令元素,对此非常问题的讲座:


  

例如,注意到我的自定义指令是如何从工作preventing NG-模型


您还可能有兴趣在这个计算器的问题,ngModel和组件带隔离范围

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

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);
     });

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);
     });

解决方案

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, "One directive's isolation scope isolates other directives on the same element," talks about this very problem:

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

You may also be interested in this StackOverflow question, "ngModel and component with isolated scope".

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

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