angularjs - Function return different response using $q.defer in controller and resolve -
i have created state using state provider , used resolve
, called api using factory.
.state('test', { url: '/test/:testid', templateurl: template_url + 'test/test.html', controller: "testcontroller", resolve: { gettest : function($stateparams,testfactory){ return testfactory.gettest($stateparams.testid); }, testid: ['$stateparams', function ($stateparams) { return $stateparams.testid; }] } }); })
i have created factory having function(gettest ) return object
gettest : function(parmtestid){ var deferred = $q.defer(); $http.get(api_url + "test/testbyid?testid="+parmtestid).success(deferred.resolve) .error(deferred.resolve); return deferred.promise; }
i have created controller, inject provider
.controller("testcontroller", function($scope,$rootscope,$http,gettest,testid, testfactory ){ console.log(gettest); // work , return json object var returnobj = testfactory.gettest(testid); //i called factory function console.log(returnobj)//its not return object return promiss }
i not able understans when call state resolve, return json object return api call when call in controller not retrun json object. return
promise {$$state: object} $$state: object status: 1 value: object __proto__: object __proto__: promise
in value have actual result want(json object) question in both case, why return different object ?
thank in advance. if dont understand discuss in comment.
it's returning promise object, waiting handled!
you may use .then(successcallback, errorcallback)
same:
var returnobj = testfactory.gettest(testid).then( function(res) { // 1st argument passed in function response object console.log(res); }, function(err) { // called in case request fails console.log('some error: ', err); } );
also, found bug in gettest
method:
$http.get(api_url + "test/testbyid? testid="+parmtestid).success(function(res) { deferred.resolve(res); }) .error(function(err) { deferred.reject(err); }); return deferred.promise;
deferred.resolve , deferred.reject methods, have call them!
Comments
Post a Comment