0

假设我有一个项目列表:

列表.html

<div ng-repeat="item in items">
  <span ng-click="editItem(item)">{{item.title}}</span><br>
</div>

我想编辑其中之一:

.controller('adminCtrl', ["$scope", "angularFireCollection", function($scope, angularFireCollection) {

  var ref = new Firebase("https://example.firebaseio.com/articles");
  $scope.items = angularFireCollection(ref);

  $scope.editItem = function(item){
    $scope.item = item;
    $scope.updateView('form');
  }

  $scope.updateItem = function(item){
    $scope.items.update(item);
    $scope.updateView('list');
  }

  $scope.goBack = function(){
    $scope.updateView('list');
  }
}]);

单击editItem(item)切换form.html部分:

表单.html

<div>
  <button type="button" ng-click="goBack()">Go Back</button>
  <input type="text" ng-model="item.title">
  <button type="button" ng-click="updateItem(item)">Save</button>
</div>

如果我编辑我当前的项目并且goBack()updateItem(item)这些更改在本地传播之前 - 但显然不应该因为我尚未更新记录。

如何将传入的 firebase 对象与其设置editItem(item)的变量分离?$scope.item

4

2 回答 2

1

Chandermani's point is correct, but you have to hack your way into clone:
var clonedItem = angular.fromJson(angular.toJson(item));

This will work!

于 2013-11-11T18:55:03.013 回答
0

尝试使用angular.clone()编辑中的方法创建项目的克隆,看看它是否有效

  $scope.editItem = function(item){
    $scope.item = angular.copy(item);
    $scope.updateView('form');
  }
于 2013-11-11T16:04:44.260 回答