0

I have a group of accounts like [{username: xxx, password: xxx} , ....], then I print them to page like below,

<tr ng-repeat = 'account in accounts'>
   <td>{{account.username}}<td>
   <td>{{account.password}}<td>
   <td><button ng-click='ResetPassword(account)'>Reset</button></td>
</tr>

The ResetPassword() func,

$scope.ResetPassword = function(account){
        $http.post('/reset/password/'+account.username).success(function(rAccount){

            account = rAccount; //account & rAccount have the same structure.
         }
    });
};

The problem is if I override account=rAccount, the password UI never been updated, unless I set the attribute manually like account.password = rAccount.password, see this JSFIDDLE

So, why it doesn't work via obj overriding? then how to fix it? my account is far more complex than this example, it's not a good practice to update one by one I think, thanks.

3 Answers 3

1

Use angular.extend

angular.extend(dst, src);

Like this

angular.extend(account, rAccount);

JSFIDDLE

Sign up to request clarification or add additional context in comments.

Comments

0

The reason it doesn't work mainly because, angularjs adds a $$hashKey property to objects in collections that are being used with ng-repeat.

$$hashkey is a auto generated key which angular assigns to array,collection passed in ng-repeat directive. Angular uses it to keep track of any changes made to array and update dom accordingly.

So in your case account and rAccount doesn't have the same structure. thats why it works for angular.extend as @anik-islam-abhi suggested. You can also use

angular.copy(source,[destination]);

Like

 angular.copy(rAccount,account); 

http://jsfiddle.net/HB7LU/20042/

Comments

0

account = rAccount does not work. You have to update $scope.accounts value

See http://jsfiddle.net/HB7LU/20043/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.