AngularJS ui-router resolve of REST resource -
i working on application photographers can use upload photos. frontend angularjs , there restfull api backend.
because of issues , fact ui-router seems better ngrouter, decided change $routeprovider $stateprovider of ui-router.
however, resolve doesn't work anymore (i guessed break cannot find solution situation).
so here original $routeprovider code:
.when('/photographer', { templateurl : '/static/partials/photographer/photographer_dash.html', controller : 'photographercontroller', resolve: { photogprepservice: function (photogservice) { return photogservice.ownphotos(); } } })
the photogservice $resource service has following $resource objects:
return $resource(apihost, {}, { 'ownphotos': { url: apihost + '/photographer_own_photos/', method: 'get', interceptor: responseinterceptor } });
in controller following (photogprepservice being injected because of resolve):
var promise = photogprepservice; promise.then( function (data) { $scope.ownphotos = data.data.photos; });
this worked , photos in scope.
however said ui-router doesn't work , cannot seem working...
according docs (https://github.com/angular-ui/ui-router/wiki#resolve) following should work:
$stateprovider.state('photographer', { url: '/photographer', templateurl: '/static/partials/photographer/photographer_dash.html', controller: 'photographercontroller', resolve: { photogprepservice: function (photogservice) { return photogservice.ownphotos(); } }
however, when resolve injected in controller , use console.log() print response, following:
resource(value)
i somehow cannot seem values (json response {"photos": ...}) injected controller.. tried various solutions have been suggested here on stackoverflow , read guides , api of ui-router, cannot wrap head around going wrong... hope can guide me in right direction..
thanks!
i think can use code below:
//the photogservice keep same before //inject 'photogservice' controller photographercontroller photogservice.ownphotos().then(function(data) { $scope.ownphotos = data.data.photos; }); //instead of injecting photogprepservice through 'resove', inject photogservice controller //for $stateprovider $stateprovider.state('photographer', { url: '/photographer', templateurl: '/static/partials/photographer/photographer_dash.html', controller: 'photographercontroller', }
Comments
Post a Comment