angular.bootstrap and angular ui-router not launch

2019-08-13 01:58发布

问题:

I'm in the process of switching an existing AngularJS mobile app from Trigger.io to Cordova, as it provides way better/more plugins for what I'm building.

Cordova requires my application to be loaded after the deviceready event is fired. My application is built on top of the ionic framework, and they provide a wrapper (window.ionic.Platform.ready) for this event. I thought it was going to be as simple as removing ng-app from my HTML and bootstrap my app when the device is ready. Alas...

I came across something weird. I'm using ui-router to handle my states and they get triggered in the $stateChangeStart, but the controller linked to that state does not get triggered at all.

I've made two Plunkrs as example;

  • the first one uses ng-app and works perfectly
  • the second one uses angular.bootstrap, but never console.logs stuff from my controllers.

Am I doing something wrong here? Any help is appreciated!

回答1:

Issue is with the selection of rootElement, you need to just get the body via document.body instead you are trying to retrieve it as document.getElementById('body') which does not return anything.

angular.element(document).ready(function () {
    angular.bootstrap(document.body, ['myApp']);
});

Plnkr