0

Watch this video,

https://www.youtube.com/watch?v=IRelx4-ISbs

You'll find that the code has a line wrote this:

$scope.twitterResult = $scope.twitter.get({q:$scope.searchTerm});

This is a litter quirk: the 'get' method of 'twitter' is obviously a async function, how does it return a value to $scope.twitterResult???

jsFiddle(Can't work cause the twitter API has changed):

http://jsfiddle.net/johnlindquist/qmNvq/

1

2 Answers 2

0

This code $scope.twitter.get({q:$scope.searchTerm}); return defer object. Angular view arranged so that view will be update when defer object resolve. It's just a convenience.

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

2 Comments

But how does the deferred object knows which variable should it change? Or the deferred object can change itself?
When Angular encounters a promise inside the view, it automatically sets up a success callback and substitutes the promise for the resulting value once it has been resolved. Using promises directly on the view is a feature of AngularJS that many people are unaware of, but it opens up a lot of opportunities for making our controllers as lean as possible. Example: plnkr.co/edit/pL4lF9JYXZ7EVJNv7PJK?p=preview
0
$scope.twitterResult = $scope.twitter.get({q:$scope.searchTerm});

Your code is good for only angular binding.

But if you need to get data on run time, you need to write this below format.

If $scope.twitter is a URL,Then write it

$http.get($scope.twitter, {q:$scope.searchTerm}).success(function (response) {
$scope.twitterResult=response;
}

$http is must defined in contoller

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.