Angularjs state resolve before controller -
i know question gets asked lot, , have read bunch of stackoverflow questions on topic nothing seems fix issue. please show me how being idiot.
using angular 1.3.0.
here state:
.state('app.admin.cards.edit', { url: '/edit/:cardid', views: { cards: { templateurl: 'tpl/cards.edit.html', controller: 'admincardeditctrl', resolve: { data: ['$stateparams', 'admincardservice', function ($stateparams, admincardservice) { var cardid = $stateparams.cardid; admincardservice.getcard(cardid).then(function (data) { return data; }); } ] } } }, access: { auth: true, admin: true } })
here controller:
.controller('admincardeditctrl', ['$scope', '$window', 'admincardservice', 'data', function ($scope, $window, admincardservice, data) { console.log(data); } );
here part of service:
.factory('admincardservice', function ($http, $q) { return { ... getcard: function (_id) { var d = $q.defer(); var promise = $http.post('/api/card', { _id: _id }).success(function (data) { d.resolve(data); }); return d.promise; }, ... }; });
the data never gets passed controller , console.log outputs "undefined". if console.log data in state or in service, working expected. doing wrong people?
just return result of admincardservice in router. router handle 'then' callback , send result of controller:
resolve: { data: ['$stateparams', 'admincardservice', function ($stateparams, admincardservice) { var cardid = $stateparams.cardid; return admincardservice.getcard(cardid);; } ] }
Comments
Post a Comment