2014-12-29 33 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' },