如何在 angularjs 中实现编辑功能的取消按钮

How to implement the cancel button for edit functionality in angularjs

我用过编辑按钮。编辑后我有保存和取消按钮。保存按钮按预期工作但不是取消按钮。 如果我在尝试编辑后单击取消按钮,它应该显示以前的文本。谁能帮帮我
js fiddle 这里 http://jsfiddle.net/F7K63/143/

 <tr ng-repeat="item in items">
        <td>
            <span ng-hide="item.editing">{{item.name}} <button  ng-click="editItem(item)">Edit</button></span>
            <input ng-show="item.editing" ng-model="item.name"  autofocus />
            <button ng-show="item.editing" ng-click="doneEditing(item)">Save</button>
             <button ng-show="item.editing" ng-click="Cancel(item)">Cancel</button>
        </td>
    </tr>

最快的选择是编辑您的 $scope.eidtItem 和 $scope.Cancel 函数。

$scope.editItem = function (item) {
    item.editing = true;
    item.oldName = item.name;
}

...

$scope.Cancel = function (item) {
    item.editing = false;
    item.name = item.oldName;
};

http://jsfiddle.net/F7K63/147/

复制您要编辑的对象。如果您按取消替换原点。参见 fiddle

$scope.editItem = function (item) {
    item.editing = true;
    item.backupName = angular.copy(item.name);
}

$scope.doneEditing = function (item) {
    item.editing = false;
    delete item.backupName;
    //do some background ajax calling for persistence...
};
$scope.Cancel = function (item) {
    item.editing = false;
    item.name = angular.copy(item.backupName);
    delete item.backupName;
};