2

I'm not sure what I'm missing here but I'm trying to update a value inside an array, and then have this reflected in the repeater. Do I need to make a copy or something before applying the update in order for Angular to show the changes?

---sample json

  "lists": [
       {
        "name": "one",
        "enabled": true
       },
       {
        "name": "two",
        "enabled": false
       }
    ]

!-- code

setTimeout(function(){
    $scope.$apply(function(){
    $scope.lists[1].enabled = true;
});
},1);

!--- html

<span data-ng-repeat="list in lists | filter: {enabled: true}"></span>
5
  • 2
    please add the html code, if you use data-ng-repeat="item in items" and $items is an array it will update Commented Jul 16, 2014 at 12:30
  • code is far too disconnected without being able to see your scope and basic html Commented Jul 16, 2014 at 12:35
  • I expect the community can solve this if you can provide a jsfiddle of your problem Commented Jul 16, 2014 at 12:37
  • the repeater is generated from another controller. So I need to access that controller by reference, in order to refresh the repeater? Commented Jul 16, 2014 at 12:40
  • 1
    the code works fine: jsfiddle.net/gHb9z Commented Jul 16, 2014 at 12:41

3 Answers 3

7

From the "other controller" you are talking about broadcast an event:

 $rootScope.$broadcast('listChange', listArray);

Then in the controller that controls your $scope.lists value, listen for the event:

  $scope.$on('listChange', function(event, list) {

       $scope.$apply(function() {
           // Update goes here
         });

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

Comments

1

It might help if you use angulars built-in timeout function $timeout.

$timeout(function(){
    $scope.lists[1].enabled = true;
},1);

Documentation here: https://docs.angularjs.org/api/ng/service/$timeout

Comments

0

Add your lists variable to the $scope inside your controller like this:

$scope.lists = [{
    "name": "one",
    "enabled": true
   },
   {
    "name": "two",
    "enabled": false
   }];

You also have to add something inside the <span>, because the Html you posted would not display anything. (You need to do something inside the <span> tag in order to show something)

<span ng-repeat="list in lists | filter:{enabled: true}">{{list.name}}</span>

Now when you update the lists variable, this will be reflected in the Html immediately. Works fine for me. Hope i could help!

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.