加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:40:07 所属栏目:安全 来源:网络整理
导读:为什么格式化程序不适用于隔离范围?这是有角度的错误还是我做错了什么? 这包含隔离范围,不起作用:http://jsfiddle.net/YbdXQ/56/ restrict: 'A',scope:{},link: function(scope,elm,attrs,ctrl) { ctrl.$formatters.unshift(function(modelValue) { conso
为什么格式化程序不适用于隔离范围?这是有角度的错误还是我做错了什么?

这包含隔离范围,不起作用: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”感兴趣.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读