I have a simple hashmap and a simple method which displays the status in text, but when I update only 1 user-status, all of them are getting updated (function is called for all users). Is there a way to update only one element and not all of them?
Example code is here, just see what happens in console when you click on the "Change Status" button.
HTML:
<div ng-app="myApp">
<div ng-controller="Ctrl">
<div class="user-panel" ng-repeat = "(id, user) in _users">
Status:
<span class="user-status{{user.status}}">
{{getStatusInText(user.status)}}
</span>
<hr>
<div class="toLeft">(<b>{{id}}</b>) {{user.name}}</div>
<div class="toRight">
<button ng-click="changeUserStatus(id, '1')">Change Status</button>
</div>
<div class="clear"></div>
</div>
</div>
</div>
AngularJS:
var app = angular.module('myApp', []);
function Ctrl($scope) {
$scope._users = {"123":{"name":"test","status":"0"}, "124":{"name":"test2", "status":"0"},"125":{"name":"test2", "status":"0"}};
$scope.getStatusInText = function(status) {
console.log("CALLED: " + status);
return (status === "1") ? "online" : "offline";
}
$scope.changeUserStatus = function(uId, status) {
$scope._users[uId].status = status;
}
}
And the fiddle for above example:
another example with iteration, which is called everytime with the getStatusInText function.