我正在实现就地编辑功能,在用户单击按钮后,字段变得可编辑。这是两个指令:
按钮指令:
.directive('editbutton', function() {
return {
restrict: 'A',
template: '<div ng-hide="editorEnabled>' +
'<button class="btn btn-small" ng-click="enableEditor(item)">' +
'<i class="icon-edit"></i>' +
'</button>' +
'</div>' +
'<div ng-show="editorEnabled>' +
'<button class="btn btn-small" ng-click="save(item)">' +
'<i class="icon-ok"></i>' +
'</button>' +
'</div>',
link: function(scope, elm, attrs, ctrl) {
console.log("butotn");
scope.editorEnabled = false;
scope.enableEditor = function(item){
scope.editorEnabled = true;
}
scope.disableEditor = function(){
scope.editorEnabled = false;
}
scope.save = function(){
// edit client side and server side
scope.disableEditor();
}
}
};
});
对于文本编辑:
angular.module('solarquote.directives', []).directive('editfield', function() {
return {
restrict: 'A',
//scope: { value:"=editfield", inherit:'@editorEnabled'},
transclude: true,
template: '<span ng-hide="editorEnabled" ng-transclude></span>' + // viewable field
'<span ng-show="editorEnabled"><input class="input-medium" ng-model="value"></span>', // editable field
link: function(scope, elm, attrs, ctrl) {
// nothing here
}
};
})
和 HTML:
<tr ng-repeat="item in items">
<td>
<a href="" ng-click="filter_fund(item)">
<span editfield="item.fields.name">{{ item.fields.name }}</span>
</a>
</td>
<td>
<span editfield="item.fields.address">{{ item.fields.address }}</span>
</td>
<td style="text-align:center" ng-show="editable_table"><span editbutton></span></td>
问题是,当我实现自己的范围以检索名称时,无法访问 editorEnabled 范围。我曾尝试使用 @ 和 = 来获取“继承”范围,但无法弄清楚如何。
目标是使用 =editfield 预填充输入字段。然后将 editorEnabled 范围与启用和禁用它的按钮共享。但是,该范围没有启用编辑器。
任何帮助,将不胜感激。