大佬教程收集整理的这篇文章主要介绍了angularjs – 为什么我无法访问正确的范围?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<!doctype html> <html> <head> </head> <body> <div ng-app="project"> <div ng-controller="mainCtrl"> {{ property1 }} <br /> {{ property2 }} <div class="ts" d-child property1="{{ property1 }}cloud" property2="property2"> property1: {{ property1 }} <br /> property2: {{ property2 }} </div> </div> </div> </body> </html>@H_607_8@JS:
angular.module('project',[]) .controller('mainCtrl',function($scopE) { $scope.property1 = 'ss'; $scope.property2 = 'dd'; }); angular.module('project') .directive('dChild',function() { return { reStrict: 'A',scope: { property1: '@',property2: '=' },link: function(scope,element,attrs) { } // template: '<input type="text" ng-model="property1" />' } })@H_607_8@我认为property1:{{property1}}将是“property1:sscloud”,但事实证明它是“ss”,好像它仍然指的是mainCtrl控制器的范围,不应该是指的范围d-child指令?
如果我在指令中使用模板,它确实引用了正确的范围并显示’sscloud’,任何人都可以告诉我为什么?
>如果指令没有模板属性(或templateUrl),则内部内容将附加到父作用域.实际上在this commit之前,内部内容正在被孤立的范围.检查您的示例以确认它适用于小于1.2的版本
>如果指令确实具有模板属性,那么它将覆盖内部内容(除非被转换).
>仅当您使用转换时,内部内容才会附加到同级作用域(非隔离).
>角度以这种方式工作的原因是为了让可重复使用的组件松散耦合,并且对您的应用程序没有任何副作用.
>没有隔离范围的指令不会从同一元素的隔离指令中获取隔离范围(see important commit).
>指令的模板无论如何都会获得孤立的范围.
如果要更改此行为,可以将隔离范围传递给tranclusion函数,如下所示:
angular.module('project') .directive('dChild',transclude: true,attrs,ctrl,linker) { linker(scope,function(clone,scopE){ element.append(clonE) }) } } })@H_607_8@我强烈建议你看看这些教程:
> Angular.js – Transclusion basics
> Angular.js – Components and containers阅读更多:
> Access directive’s isolate scope from within transcluded content
> https://github.com/angular/angular.js/wiki/Understanding-Scopes
以上是大佬教程为你收集整理的angularjs – 为什么我无法访问正确的范围?全部内容,希望文章能够帮你解决angularjs – 为什么我无法访问正确的范围?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。