I use ng-repeat
to render a html table
. My controller looks like so:
app.controller("fooCtrl", function($scope, WebSocket) {
$scope.foo = [ ... ];
WebSocket.start(function(data) {
// this is a callback on websocket.onmessage
// here is a for loop iterating through $scope.foo objects and updating them
});
});
As you can see, I am updating the $scope.foo
array periodically. However, my view is constructed like so:
<tr ng-repeat="item in foo">
...
</tr>
The problem is, when I update foo
, it doesn't re-render my table with new data.
How would I go about this problem?
Solution 1. There are two mistakes in your code: In your table, you have to wrap the properties between {{ and }}, for example {{employee. Fname}} instead of just employee.
You can use unique filter while using ng-repeat . If you use track by $index then unique won't work. ok, I used unique and its working now, thanks!
Definition and Usage The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.
Stay on the bleeding edge of your favorite technologies. $index is a way to show which iteration of a loop you're in. If we set up an ng-repeat to repeat over the letters in 'somewords', like so: <div ng-app="app"> <div ng-repeat="item in 'somewords'.split('')"> {{$index + 1}}. {{item}} </div> </div>
Did you wrap the update in
$scope.$apply(function() {
// update goes here
});
? This should solve the problem.
You probably need to call $apply()
to force angular to do a digest cycle when you update the list, like:
WebSocket.start(function(data) {
$scope.$apply(function(){
/* your stuff goes here*/
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With