angularjs – Angular.限制:’A’指令.传递对象
发布时间:2020-12-17 17:58:00 所属栏目:安全 来源:网络整理
导读:有没有办法将配置对象传递给定义为属性指令的自定义指令? 我在Controller中有一个对象要发送到指令: $scope.configObject = { count: 5,mode: 'fast',getData: myService.getData // function of external service that avaliable in controller} 在我的视
有没有办法将配置对象传递给定义为属性指令的自定义指令?
我在Controller中有一个对象要发送到指令: $scope.configObject = { count: 5,mode: 'fast',getData: myService.getData // function of external service that avaliable in controller } 在我的视图中我声明了指令: <div class='list-class' my-list='configObject'></div> 指令看起来像: return { restrict: 'A',link: function(scope,elem,attrs) { var config = angular.fromJson(attrs.myList); } } 我试图使用angular.getJson获取配置对象 – 但它不适用于函数(它可能只获得计数和模式). .getJson()获取配置的方法不正确吗? 另外(我猜它甚至不可能) – 有没有办法让配置对象避免访问 attrs.myList 直?我的意思是如果我改变指令的初始化 .directive('myList',function() { ... }) to .directive('myCustomList',function() { ... }) 我应该改变访问权限 attrs.myCustomList 因为视图看起来像 <div class='list-class' my-custom-list='configObject'></div> 解决方法
您可以使用$parse服务来获取配置对象.
(function(){ var directiveName = 'myList'; angular.module('YourModule').directive(directiveName,['$parse',function($parse){ return { restrict: 'A',attrs) { var config = $parse(attrs[directiveName])(scope); } }; }]); })(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |