0

I am trying to pass an array of JSON objects into my directive and then repeat them back using ngrepeat, however it is not working and it is not clear why.

I have created a jsfiddle which shows the problem that I am experiencing.

https://jsfiddle.net/HB7LU/15997/

Is anybody able to tell me what I am doing wrong? I don't understand why it won't let me do this.

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

myApp.directive('dir', function() {
    return {
        restrict: 'E',
        scope:{
            items: '@'
        },
        replace: true,
        template: '<div><h6>descriptions below</h6><div ngrepeat="x in items"><p>{{x.desc}}</p></div></div>',
        link :function(scope){
            console.log(scope.items);
        }
    };
});

1 Answer 1

1

Firstly, you have a typo in your template. Its ng-repeat and not ngrepeat - notice the "-".

Secondly, you have defined your scope with the @ property. This causes the data that you pass to be in the form of a string and not the original type.

You have two choices here. Either parse the string to its original type using JSON.parse() or use the = property type which passes the data to the scope in its original format.

Working Fiddle

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

1 Comment

Thank you the ngrepeat was a bug that cropped up in my POC rewrite thank you very much changing the @ which I thought only meant read only to an = was the answer.

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.