Angularjs 有棱角的限制:';A';指令。传递对象

Angularjs 有棱角的限制:';A';指令。传递对象,angularjs,angularjs-directive,Angularjs,Angularjs Directive,是否有方法将配置对象传递到定义为属性指令的自定义指令中 我在控制器中有一个要发送到指令的对象: $scope.configObject = { count: 5, mode: 'fast', getData: myService.getData // function of external service that avaliable in controller } <div class='list-class' my-list='configObject'&g

是否有方法将配置对象传递到定义为属性指令的自定义指令中

我在控制器中有一个要发送到指令的对象:

$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>
在我看来,我声明:

$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>
我曾尝试使用angular.getJson获取配置对象,但它不适用于函数(可以只获取计数和模式)。.getJson()是获取配置的不正确方法吗

还有(我想这根本不可能)-有没有办法让配置对象避免访问

attrs.myList
attrs.myCustomList
直接的?我的意思是如果我改变指令的初始化

.directive('myList', function() { ... }) to
.directive('myCustomList', function() { ... })
我要换到办公室吗

attrs.myList
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',
            link: function(scope, elem, attrs) {
                var config = $parse(attrs[directiveName])(scope);
            }
        };
    }]);
})();

如果需要,可以使用隔离作用域传递它

return {
    restrict: 'A',
    scope: { config : '=myList' }
    link: function(scope, elem, attrs) {
        //access using scope.config
    }
}
或者,正如已经回答的,您可以从attrs解析它

      $parse(attrs["myList"])(scope);
是的,如果您将指令更改为myCustomList,则必须更改代码

    scope: { config : '=myCustomList' }


您可以$eval该属性

 link: function(scole, element, attrs) {
      var config = scope.$eval(attrs['myList']);
  }

谢谢你的详细解释,这绝对有帮助!感谢您没有建议将$eval和$parse作为首选