2014-12-29 35 views
2

我有通过这个复杂的对象阵列通过以一个指令的属性的麻烦。我做错了什么,为什么错了?角JS和传递复杂对象数组作为属性来指示

jsfiddle

剪切和粘贴代码

<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"}]; 
}); 

回答

5

你迭代foos在你的指令模板。你没有在FOOS通,你obj通过。试试这个:

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

Updated fiddle.

3

没有名为foos scope属性,它应该根据自己的范围定义scope: { obj: '=' }obj

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

或者你也可以改变范围配置到:

scope: { foos: '=obj' },