json - AngularJS routes and Ajax data on route change -
i failing understand angular way following...
i have router configured, once route changed, , templateurl supplied make ajax call fetch json data.
notice that, not want wait fetch template , use controller fetch json data because 2 serial http calls. want fetch json data in parallel fetching template. there known pattern this?
correct me if wrong... now, understand should use resolver in call "data provider", fire http call json data. data available controller. this...
app.config(['$routeprovider', 'data', function( routeprovider, dataprovider ) { routeprovider. when('/reports/:reportname', { templateurl: function( urldata ) { return "/some/url/" + urldata.reportname + "/content"; }, resolve: { reportdata: function() { return dataprovider.getdata(); } }, controller: 'reportroutingctrl' }); }]); app.controller('reportroutingctrl', ['$scope', 'reportdata', 'reportname', function( scope, reportdata ) { console.dir( reportdata ); }]);
is correct pattern follow? if so, how can access 'urldata' object in resolver?
thank help!!
this how (and think it's accepted way it).
imagine rendering view user can visualize 1 concrete item, , have fetch data item server.
i have service
one:
services.factory('itemloader', ['item', '$route', '$q', function(item, $route, $q) { return function() { var delay = $q.defer(); item.get({id: $route.current.params.itemid}, function(item) { delay.resolve(item); }, function() { delay.reject('unable fetch item' + $route.current.params.itemid); }); return delay.promise; }; }]);
my routeprovider
this:
.when('/view/:itemid', { controller: 'viewctrl', resolve: { item: ["itemloader", function(itemloader) { return itemloader(); }] }, templateurl:'/views/viewitem.html' })
and signature of controller this:
app.controller('viewctrl', ['$scope', '$location', 'item', function($scope, $location, item) { ... }]);
notice resolve injecting fetched item
controller.
Comments
Post a Comment