1

I'm trying to retrieve data from Google books API - https://www.googleapis.com/books/v1/volumes

Here's the plnkr link- https://plnkr.co/edit/K4f7CVIeurAUMRzQYMb0?p=preview

I enter the ISBN number(say: 1234) in the search text, and update the bookList with the response items that I received from google.

I'm trying to iterate over these books using ng-repeat but it's not updating. I need to click on search twice to see the result in ng-repeat.

I'm confused why isn't it updating the content dynamically.

See the plnkr link. https://plnkr.co/edit/K4f7CVIeurAUMRzQYMb0?p=preview

Enter 1234 in the search text and click search, won't update in the html.

Click search again, it updates the html with the last response.

Thanks.

5
  • You should not use $.ajax(), use $http instead, basically your approach is wrong. Commented Mar 3, 2016 at 20:09
  • also, the ng-repeat should be on the li element, not the ul Commented Mar 3, 2016 at 20:09
  • Ok, yeah, I'll do that. But any idea, why it's not updating. Commented Mar 3, 2016 at 20:11
  • 1
    you use $http like this: $http.get(url).then(function(response) { first.books = response.data; }, function(error){console.log(error)}. This way as the scope is evaluated and after the http promise is resolved your repeater will update with the result. Then your li should be <li ng-repeat="book in first.books"> Commented Mar 3, 2016 at 20:12
  • Hi, It worked. But I still didn't get the point of not using $.ajax method here. Commented Mar 3, 2016 at 20:39

1 Answer 1

1

You are using jQuery to make your call, outside angular $digest. You should use $http service instead.

$http({
  url: "https://www.googleapis.com/books/v1/volumes",
  method: 'GET',
  params: {q:first.inputISBN}
});

https://plnkr.co/edit/VkeBZL8ZF6gfYQ9iQoFv

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

3 Comments

Hi, Thanks for the answer. Can you please elaborate on your statement about Angular $digest. Or provide any link to read about that.
Angular needs to be in $digest phase to see that something (in this case array of books) changed. Many things trigger $digest loop, like $http responses, ngClick, $timeout etc. You can also trigger it manually with $scope methods: $apply, $eval, $evalAsync. Then and only then angular starts dirty-checking and reacts to changes.
Thanks, that makes sense now.

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.