11

I've created this fiddle to show my issue...

http://jsfiddle.net/dQDtw/

I'm passing a newly created array to a directive, and everything is working out just fine. However, I'm getting an error in the console window indicating:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

Any thoughts on what I need to massage to clean this up? I'd like to be able to reuse the directive without the need to update the controller.

Here is the html

<body ng-app="myApp">
    <test-dir fam-people='[1,4,6]'> </test-dir>
    <test-dir fam-people='[2,1,0]'> </test-dir>
</body>

Here is the JS.

var myApp = angular.module('myApp', []);

myApp.directive('testDir', function() {
            return { restrict: 'E'
                   , scope: { famPeople: '=famPeople' }
                   , template: "<ol> <li ng-repeat='p in famPeople'> {{p}}"
                   };
    });

3 Answers 3

15

That error is because your directive is not able to interpret the array as an array, Try this:

<body ng-app="myApp" ng-controller="ctrl1">
    <test-dir fam-people='people'> </test-dir>

</body>



var myApp = angular.module('myApp', []);

myApp.directive('testDir', function() {
                return { restrict: 'E'
                       , scope: { famPeople: '=' }
                       , template: "<ol> <li ng-repeat='p in famPeople'> {{p}}"
                       };
        });

Controller and directive:

myApp.controller("ctrl1",function($scope){
$scope.people=[1,4,6];
});

EDIT

or you could pass it in as an attribute and parse it to an array:

<body ng-app="myApp" >
    <test-dir fam-people='[1,4,6]'> </test-dir>

</body>

Directive:

var myApp = angular.module('myApp', []);

myApp.directive('testDir', function() {
                return { restrict: 'E', 
                        //scope: { famPeople: '=' },
                       template: "<ol> <li ng-repeat='p in people track by $index'> {{p}}",
                        link:function(scope, element, attrs){
                      scope.people=JSON.parse(attrs.famPeople);
                        }
                       };
        });

See fiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that would probably work. But I'd like to be able to reuse the directive without updates to a controller. I've updated my questions to be more specific
Thanks, I think the edit is more in line with what I'm looking for.
14

JSON parse doesn't work as effective when the array contains string.

For example:

<file-handler fh-services="['BOX','DROPBOX']"></file-handler>

In the directive you can use scope.$eval to convert what appears in the attribute to an array.

scope.$eval(attrs.fhServices)

2 Comments

I agree that this looks like the more proper solution when you aren't using data-binding.
This is exactly what I needed for a requirement I was working on. Thanks for posting!
0

what about:

<body ng-app="myApp">
    <test-dir fam-people='1;4;6'> </test-dir>
    <test-dir fam-people='2;1;0'> </test-dir>
</body>

and

    var myApp = angular.module('myApp', []);

    myApp.directive('testDir', function() {
                return { restrict: 'E', 
                        //scope: { famPeople: '=' },
                       template: "<ol> <li ng-repeat='p in people track by $index'> {{p}}",
                        link:function(scope, element, attrs){
                      scope.people= attrs.famPeople.split(';');
                        }
                       };
        });

cleanest solution?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.