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'を出力しませんが、リストを出力したりログに記録したりしている間は正常に動作します。なぜ :( ??


アップデート:

ng-app="myModule"ディレクティブを使用しなかったため、角度ブートストラップ関数を使用してモジュールを登録することが問題でした。

ng-app="myModule"ディレクティブを使用するか、var module = angular.module("ng");モジュールに挿入すると、うまくいくはずです。

4

2 に答える 2

1

ドキュメントには、手動のブートストラップでそれを行う必要があると書かれています:

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