javascript - Updating array items angularfire -
i using angularfire $add method add basic js objects of form {id:integer,name:name} now, if want update particular item (which has firebase-assigned key "-jeca_f70efhbki5js7j" or something, impression should use $save method. here how trying this:
$scope.chosencolors = $firebase(mychosencolorsref); $scope.updatecolor = function(data){ //data js object {id:'id',name:'name'} if($scope.chosencolors.$getindex().length>0){ var keys = $scope.chosencolors.$getindex(); keys.foreach(function(key, i) { if($scope.chosencolors[key].id!=data.id){//if id matches want update name $scope.chosencolors[key] = {id:data.id,name:data.name} $scope.chosencolors.$save[key]; return; } }); }else{ $scope.chosencolors.$add(data); }
but doesn't appear have effect on firebase...any ideas doing wrong?
firstly, should call $save
method instead of accessing key:
$scope.chosencolors.$save(key);
secondly, you're iterating on keys figure out 1 want save pretty inefficient. should change updatecolor argument take key
argument.
<div ng-repeat="(key, color) in chosencolors"> <a ng-click="updatecolor(key, color)">update</a> ... </div> function updatecolor(key, data) { $scope.chosencolors[key] = data; $scope.chosencolors.$save(key); }
Comments
Post a Comment