2

I am having trouble passing this complex object array through an attribute to a directive. What I am doing wrong and why is it wrong?

jsfiddle

Cut and pasted code

<div ng-controller="MyCtrl">
    <pass-object obj="obj"></pass-object>
</div>

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

myApp.directive('passObject', function() {
    return {
        restrict: 'E',
        scope: { obj: '=' },
        template: '<div ng-repeat="foo in foos">Hello, {{foo.prop}}!</div></div>'
    };
});

myApp.controller('MyCtrl', function ($scope) {
    $scope.obj = [{ prop: "hello" }, {prop: "world"}];
});

2 Answers 2

5

You are iterating over foos in your directive template. You didn't pass in foos, you passed in obj. Try this:

myApp.directive('passObject', function() {
    return {
        restrict: 'E',
        scope: { obj: '=' },
        template: '<div ng-repeat="o in obj">Hello, {{o.prop}}!</div></div>'
    };
});

Updated fiddle.

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

Comments

3

There is no scope property named foos, it should be obj according to your scope definition scope: { obj: '=' }:

template: '<div ng-repeat="foo in obj">Hello, {{foo.prop}}!</div></div>'

Or you can change scope config to:

scope: { foos: '=obj' },

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.