2013-08-28 112 views
0

我試圖用NG-重複指令中的模板是這樣的:NG重複不會工作

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-rep吃不打印'列表', 打印列表或記錄它,它工作正常,爲什麼:(?


更新:

的問題是使用的角度引導功能註冊模塊,因爲我沒有使用ng-app="myModule"指令。

如果我使用ng-app="myModule"指令或注入var module = angular.module("ng");模塊它應該做的伎倆。

+0

你有'
'下'{{列表之後未封閉的串} }' –

+0

對不起,這是打字錯誤,但這不是問題。 – Ayman

回答

1

確保將角引導包裝在文檔就緒功能中。

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

結帳這裏:http://jsfiddle.net/CRWAv/

+0

非常感謝,但爲什麼當打印列表工作,並重復它沒有?這是一個錯誤? – Ayman

1

Documention說,你所要做的是手動引導:

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

謝謝,它的工作 – Ayman