I’ve hacked away at it a bit. Here is a working example:
I THINK the problem with your original code is simply a timing issue. I don’t think the Modal service is instantiated by the time the user.notAuthorized
is broadcasted. So, it’s not able to call the method that does not yet exist.
I’ve put in a very hackish timeout to allow the modal to get instantiated.
UPDATE : Ignore the stuff below about using resolve. It’s actually a good way to deal with async account validation, but it doesn’t help in your example. I tried another example getting rid of broadcast and just manually checking the Account status with UserService.isAuthorized()
. Again, get the modal does not exist issue. So, it’s back to simply being a timing issue. I think the calls to $scope.openModal
are occurring in the digest cycle before the Modal has been created. So, the delay seems the only fix I can think of.
However, I don't think using broadcast is the best way to do this. If you are using ng-route, you would be best to do a resolve on the route. The resolve should check the logged in status. Then, in your controller, the logged in status will have already been determined. Then, you check it, and launch the modal.
I THINK that would solve the problem.
Here is an example of what using the resolve looks like :
angular.module('myApp', ['ionic', 'ngRoute', 'ngAnimate','AccountService'])
.config(['$sceDelegateProvider', '$compileProvider', function ($sceDelegateProvider, $compileProvider){
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https):/);
$compileProvider.imgSrcSanitizationWhitelist(/^\s*(https):/);
$sceDelegateProvider.resourceUrlWhitelist(['self']);
}])
.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
// All routes for the app
$routeProvider.when('/home', {
templateUrl: 'templates/intro.html',
controller: 'IntroController',
resolve : {
loadData: function(AccountService) {
return AccountService.initialize();
}
}
});
// if none of the above routes are met, use this fallback
// which executes the 'AppCtrl' controller (controllers.js)
$routeProvider.otherwise({
redirectTo: '/home'
});
}]);