javascript - AngularJS $routeParams resolve and promises -
so... i'm trying learn angular stuff , reached point i'm stuck. in app i'm using third party apis cannot rely on $http, that's why you'll see $apply in code.
so, have this:
.when('/dashboard',{ controller : 'dashboardcontroller', templateurl : 'ui/modules/dashboard/dashboard.html', resolve : { data : function(myservice){ return myservice.start(); } } }) so far nothing strange. method supposedly have promise, right? start function follows:
var start = function(){ return initializeconnection().then(function(){ return login(false); }); }; so, basically, if works, works. that's great. thing that, if promise rejection on second function (that's login) i'm not able catch error in this, appcontroller created on top of app (so it's being instantiated):
$rootscope.$on("$routechangeerror", function (event, previous, current, rejection) { console.log('failing'); }); that's never called. solution tried works, return promise start function, if of async functions fails, fail returned promise. see it, should unnecesary.
can point me in right direction?
in code, if promise rejection dashboardcontroller never instantiated , view never loaded - not can there. need have controller instantiated if promise rejected.
i think create controller handle global events $routechangeerror. i've created 1 below called checkconnection , assigned root element of app .
var myapp = angular.module('myapp', []); myapp.config(function($routeprovider){ $routeprovider .when('/dashboard',{ controller : 'dashboardcontroller', templateurl : 'ui/modules/dashboard/dashboard.html', resolve : { data : function(myservice){ return myservice.start(); } } }) }); myapp.controller('checkconnection', function ($rootscope){ $rootscope.$on("$routechangeerror", function (event, previous, current, rejection) { console.log('failing'); }); }); myapp.controller('dashboardcontroller', function($rootscope) { // code controller goes here }); and in index.html have:
<div ng-app="myapp" ng-controller="checkconnection"> <ng-view></ng-view> </div> the controller checkconnection going instantiated anyway - can rely on handle error.
if no error happens specific controller (e.g: dashboardcontroller) route accessing executed , view loaded.
there room improvements think work you.
hope helps or @ least point right direction.
Comments
Post a Comment