该错误是因为您的指令无法将数组解释为数组,请尝试以下操作:
<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}}" }; });控制器和指令:
myApp.controller("ctrl1",function($scope){$scope.people=[1,4,6];});编辑
或者您可以将其作为属性传递并解析为数组:
<body ng-app="myApp" > <test-dir fam-people='[1,4,6]'> </test-dir></body>
指示:
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); } }; });见小提琴。



