Angular Limit: "A". Skip Object

Is there a way to pass the configuration object into a custom directive that is defined as an attribute directive?

I have an object in the controller that I want to send to the directive:

$scope.configObject = {
    count: 5,
    mode: 'fast',
    getData: myService.getData // function of external service that avaliable in controller
}

In my view, I declare a directive:

<div class='list-class' my-list='configObject'></div>

The directive is as follows:

return {
    restrict: 'A',
    link: function(scope, elem, attrs) {
        var config = angular.fromJson(attrs.myList);
    }
}

I tried to get the configuration object using angular.getJson - but it does not work for functions (it is possible to get only the score and mode). Is .getJson () the wrong way to get the configuration?

Also (I think this is not even possible) - is there a way to get the configuration object, avoiding access to

attrs.myList

directly? I mean, if I change the directive initialization from

.directive('myList', function() { ... }) to
.directive('myCustomList', function() { ... })

I have to change access to

attrs.myCustomList

because the view will look like

<div class='list-class' my-custom-list='configObject'></div>
+4
3

$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);
            }
        };
    }]);
})();
+2

, ,

return {
    restrict: 'A',
    scope: { config : '=myList' }
    link: function(scope, elem, attrs) {
        //access using scope.config
    }
}

, attrs

      $parse(attrs["myList"])(scope);

, myCustomList,

    scope: { config : '=myCustomList' }

      $parse(attrs["myCustomList"])(scope);
+8

You can $ eval attribute

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

Source: https://habr.com/ru/post/1569598/


All Articles