0

我正在尝试在指令模板中使用 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");模块,它应该可以解决问题。

4

2 回答 2

1

请务必将角度引导程序包装在文档就绪功能中。

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

在这里结帐:http: //jsfiddle.net/CRWAv/

于 2013-08-28T18:08:20.500 回答
1

文档说您必须在手动引导程序上执行此操作:

angular.element(document).ready(function() {
  angular.bootstrap(document, ['myModule']);
});
于 2013-08-28T18:16:55.170 回答