Hi, thanks for the answer.
You were right about returning the defer object, problem solved !
However, i still can't set a variable in my controller, it don't
understands "this."
app.factory('tmdb', ['$http', '$q', '$log', function($http, $q, $log) {
return {
movie: function(id) {
var deffered = $q.defer();
$http.get('https://api.themoviedb.org/3/movie/'+id+'?api_key=3b21647fbda0a5b52703c04d6e74169e&language=fr').then(function(response)
{ //promise
deffered.resolve(response);
});
return deffered.promise;
}
};
}]);
app.controller('MovieCtrl', ['$http', '$log', 'tmdb', function($http, $log,
tmdb){
this.getMovie = function(id) {
tmdb.movie(id).then(function(response) {
console.log(response);
this.movie = response.data; // TypeError: Cannot set property 'movie'
of undefined
console.log(this.movie);
});
};
this.getMovie(504);
}]);
Thanks again!
Le vendredi 18 décembre 2015 08:09:56 UTC+1, Dominik Mank a écrit :
>
> You need to return in your Factory the defer object. Currently you are
> returning nothing, thtas why its undefinied
>
> i've made an example, hopefully you will understand it. (But really, i
> think theres only missing in your factory the
>
> return defferred.promise;)
>
> http://plnkr.co/edit/WbfIwE0hX8AToubsTsKs?p=preview
>
>
>
> Am Freitag, 18. Dezember 2015 04:00:30 UTC+1 schrieb Garwan50:
>>
>> Hi,
>>
>> i'm trying to communicate with themoviedb API.
>>
>> I have to use promise but clearly i dont know how to use them.
>>
>> I want to set a property of my controller with the data i get.
>>
>> I use a factory service :
>>
>> app.factory('tmdb', ['$http', '$q', '$log', function($http, $q, $log) {
>> return {
>> movie: function(id) {
>> var deffered = $q.defer();
>> $http.get('
>> https://api.themoviedb.org/3/movie/'+id+'?api_key=3b21647fbda0a5b52703c04d6e74169e&language=fr').then(function(response)
>>
>> {
>>
>> $log.info(response); // what i want
>>
>> $log.info(deffered.resolve(response)); // undefined :c
>> return deffered.resolve(response); // promise
>> });
>> }
>> };
>> }]);
>>
>>
>> so it returns a promise of a result.
>>
>> app.controller('MovieCtrl', ['$http', '$log', 'tmdb', function($http,
>> $log, tmdb){
>> var promise = tmdb.movie(504);
>> promise.then(function(response) { // error Uncaught TypeError: Cannot
>> read property 'then' of undefined
>> this.movie = response;
>> console.log(this.movie);
>> });
>> }]);
>>
>>
>> and in my controller i fetch data for movie id 504.
>>
>> but the movie method returns a undefined type, however, if i
>> $log.log(response), it gives me the data i want.
>>
>> promise and http request are new to me, so i guess i don't know how to
>> use it yet, i've been looking for a solution all day but nothing.
>>
>> Tell me if you need more information,
>> Thanks!
>>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.