我试图创建一个指令,其中数据显示在表格布局中。每行都有一个编辑按钮,点击编辑按钮时,只有该行应该处于编辑模式。但在我的情况下,所有的行都以编辑模式显示。 这里是demo。指令模板中的重复ng内项目的隔离范围
这里是指令代码:
.directive('myGrid', function() {
return {
restrict: 'E',
scope: {
employees: '='
},
controller: function($scope) {
$scope.isEdit = false;
$scope.showEdit = function() {
$scope.isEdit = $scope.isEdit == true ? false : true;
}
},
template: '<table class="table">' +
'<thead>' +
'<tr>' +
'<th ng-repeat="(key,value) in employees[0]">{{key}}</th>' +
'</tr>' +
'</thead>' +
'<tbody>' +
'<tr ng-repeat="emp in employees">' +
'<td><span ng-hide="isEdit">{{emp.FirstName}}</span><input type="text" ng-show="isEdit" ng-model="emp.FirstName" class="form-control"></td>' +
'<td><span ng-hide="isEdit">{{emp.LastName}}</span><input type="text" ng-show="isEdit" ng-model="emp.LastName" class="form-control"></td>' +
'<td><span ng-hide="isEdit">{{emp.Email}}</span><input type="text" ng-show="isEdit" ng-model="emp.Email" class="form-control"></td>' +
'<td><span ng-click="showEdit()" ng-class="{\'glyphicon glyphicon-edit\':isEdit==false,\'glyphicon glyphicon-ok\':isEdit==true}"></span></td>' +
'</tr>' +
'</tbody>' +
'</table>'
};
})
我还建议改变'NG-show'和'NG-hide'为'NG-if'这是更好的性能。请参见该指令模板在我的回应 –
这里'纳克-show'和'ng-hide'优于'ng-if',因为重新创建DOM代价高昂,而且ng-if具有scope,但是ng-show和ng-hide没有范围。 –
如果使用'ng-if',只有当ng-if表达式评估为真时,才会加载内容。 –