我正在尝试在指令模板中使用 ng-repeat,如下所示:
JS
var module = angular.module("myModule", []);
module.directive('direct', function ()
{
return {
restrict: 'E',
controller: "directController",
// This HTML will replace the direct directive.
replace: true,
transclude: true,
scope: {title: "@"},
template:
'<div>'
// print: this is a title, which is correct
+ '{{title}}'
+ '<br>'
// print: ["1","2","3"], which is correct
+ '{{list}}'
+ '<br>'
// doesn't print anything, why ?!
+ '<div ng-repeat="l in list">{{l}}</div>'
+ '</div>',
link: function (scope, element, attrs, controller)
{
// scope has list in it
console.log(scope);
}
}
});
module.controller('directController', ["$scope", function ($scope)
{
$scope.list = ["1", "2", "3"];
}]);
angular.bootstrap($('html'),['myModule']);
HTML
<direct title="this is a title"></direct>
结果 HTML
<div>
this is a title
<br>
["1","2","3"]
<br>
</div>
如上所示,ng-repeat 不打印'list',在打印 list 或记录它时,它工作正常,为什么 :( ??
更新:
问题是使用角度引导函数来注册模块,因为我没有使用该ng-app="myModule"
指令。
如果我使用ng-app="myModule"
指令或注入var module = angular.module("ng");
模块,它应该可以解决问题。