0

How can I make the binding works for object properties. For example in my controller I have:

$scope.reviews = {
  rating_summary: 4,
  items: [ { title: 'A Title'}, etc...  ]
}

And in my view:

<li ng-repeat="review in reviews.items">

Now, whenever i change the reviews variables in the controller, nothing is updated:

$scope.reviews = [new updated reviews]

Probably beacause angular is listening for changes on the old reference of $scope.reviews.

There are two solutions for the binding to work, but both are not very clever:

  1. Attach the items to the scope directly, so it would be $scope.items = $scope.reviews.items, Now whenever the items are changed they would be updated.
  2. User angular.copy to keep the destination reference, so angular.copy({items: []}, $scope.reviews);

Are there any other solutions to accomplish this other than the above.

4
  • It should work like #scope.reviews = []new updated reviews]... the problem should be somewhere else... and could u create a plunker for demonstrate... Commented Oct 23, 2016 at 5:59
  • it would have worked if scope.reviews is an array, but it's an object and the array is one of the object key, i.e. $scope.reviews = { rating_summary: 5, items: [ item1, item2, ..] } Commented Oct 23, 2016 at 6:04
  • Check my example: jsfiddle.net/or66quta Commented Oct 23, 2016 at 6:06
  • @PoyrazYilmaz sorry man my bad Commented Oct 23, 2016 at 6:33

1 Answer 1

1

It should work there must be some mistake while updating the object/items Please refer this Plunker

$scope.reviews = {
    rating_summary: 4,
    items: [ { title: 'A Title'}, { title: 'B Title'}  ]
  };

  $scope.changeItems = function(){
    $scope.reviews.items = [ { title: 'A Title'}, { title: 'B Title'},
                             { title: 'C Title'}];
  };

  $scope.changeObject = function(){
    $scope.reviews = {
    rating_summary: 4,
    items: [ { title: 'A Title'}, { title: 'B Title'}, { title: 'D Title'}  ]
  };
Sign up to request clarification or add additional context in comments.

1 Comment

it actually does work, I had problems with other parts of the code, sorry

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.