javascript - How to make a failed routing resolve promise load a different template URL? -
learning angular , running issue cant seem find "good" answer to. i've been following official tutorial , have similar setup project im working on.
the $routeprovider .when case phone details gets specific .json file based on name of phone clicked on in phone list page. problem is, if type in phone name doesn't have .json file associated it, route still proceeds , shows blank page. i'm trying figure out how prevent this.
i've gotten far add resolve .when route. however, can prevent view changing or use $location.path redirect. neither of these seem ideal.
how go showing "404 page" view phone doesn't have json file? in head, show different templateurl if request responds 404, cant seem working.
any great!
here code i've got far:
my route handling.
.when('/game/:game', { templateurl: 'views/game-detail.html', controller: 'gamedetailctrl', resolve: { mydata: ["games", "$location", "$q", "$timeout",'$rootscope', function(games, $location, $q, $timeout,$rootscope) { var def = $q.defer(); var path = $location.path().tostring().substring(6); // games service games.get({gamename: path}, function(game, s) { }).$promise.then( function(data) { // found game data def.resolve(data); }, function(error) { // couldn't find json file def.reject(error); } ); return def.promise; }] } }).when('/pagenotfound', { templateurl: 'views/error.html' })
root scope route change error listener:
$rootscope.$on("$routechangeerror", function(event, current, previous, rejection) { console.log("failed change routes"); //$location.path('/pagenotfound'); });
well, don't way, in case have template html contain template both success , failure scenario , ng-switch inside flip states when call fails or succeeds.
also, think work if tried copy idea directives well: angular.js directive dynamic templateurl
Comments
Post a Comment