【发布时间】:2014-06-02 03:52:17
【问题描述】:
我有一个循环调用的指令。循环中的每个项目都有一个 FieldTypeId 属性,并且根据 FieldTypeId 的值,我想换出模板的 URL。我觉得这是一种更好的多态方法,而不是在 html 中执行 ng-switch 语句。
<div ng-repeat="item in attributes">
<div attribute-input></div>
</div>
当然,$scope 在此指令中不可用:
editStudentAccountModule.directive('attributeInput', function () {
return {
restrict: "AE",
templateUrl: function () { //
var attribute = $scope.attributes[$scope.$index];
if (attribute.FieldTypeId == 1) {
return '../Templates/dropdown.html';
} else if (attribute.FieldTypeId == 2) {
return '../Templates/radio.html';
} // etc.
}
}
});
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope