我有以下设置:
应用程序/指令
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>
这是一个非常简单的示例,但我无法渲染它。也许有一些教程没有告诉我,或者那是秘密的 Angular 知识?
如果我删除<tr ng-repeat="..." />
and place内的指令<td>{{ displayText }}</td>
,它将显示所有记录。
但我希望该指令比单个<td>{{}}</td>
(最终)更复杂,以便我可以在多个应用程序中重用该指令。
所以,我真的在问我们如何正确地创建一个进入 ng-repeat 的指令?我错过了什么?应该从上面的代码中删除什么?