我有以下設置:角指令不評估內部NG-重複
應用程序/指令
var app = angular.module("MyApp", []);
app.directive("adminRosterItem", function() {
return {
restrict: "E",
scope: {
displayText: "@"
},
template: "<td>{{ displayText }}</td>", // should I have this?
link: function(scope, element, attrs){
// What do I put here? I don't seem to have any
// element to initialize (set up event handlers, for example)
},
compile: function(?,?,?){} // should I have this? If so, what goes inside?
}
});
控制器
function PositionsController($scope) {
$scope.positions = [{ Name: "Quarterback", Code: "QB" },
{ Name: "Wide Receiver", Code: "WR" }
];
}
HTML:
<div ng-app="MyApp">
<div ng-controller="PositionsController">
<table>
<tr ng-repeat="position in positions">
<admin-roster-item displayText="{{ position.Name + ' (' + position.Code + ')' }}"></admin-roster-item>
</tr>
</table>
</div>
</div>
這是一個非常簡單的例子,但我可以不要讓它渲染。也許有些教程沒有告訴我,或者那是祕密的角度知識?
如果我刪除<tr ng-repeat="..." />
中的指令並將<td>{{ displayText }}</td>
替換掉,它將顯示所有記錄。
但我希望指令比單個<td>{{}}</td>
更復雜(最終),以便我可以在多個應用程序中重用此指令。
所以,我真的在問我們如何正確地創建一個在ng-repeat內部的指令?我錯過了什麼?應該從上面的代碼中取出什麼?
AngularJS的跟蹤器存在一個問題:https://github.com/angular/angular.js/issues/1459 –