ng-repeat在指令模板中不起作用

时间:2013-08-28 16:57:11

标签: angularjs angularjs-directive angularjs-ng-repeat

我试图在指令模板中使用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', 打印列表或记录它,它工作正常,为什么:( ??


更新

问题是使用angular bootstrap函数来注册模块,因为我没有使用ng-app="myModule"指令。

如果我使用ng-app="myModule"指令或注入var module = angular.module("ng");模块,它应该可以解决问题。

2 个答案:

答案 0 :(得分:1)

务必将角度引导程序包装在文档就绪函数中。

$(document).ready(function() {
    angular.bootstrap($('html'),['myModule']);
});

结帐处:http://jsfiddle.net/CRWAv/

答案 1 :(得分:1)

Documention表示您必须在手动引导程序上执行此操作:

angular.element(document).ready(function() {
  angular.bootstrap(document, ['myModule']);
});