在开始 Angular JS 之前等待异步任务完成

Wait for Async Task to Finish Before starting Angular JS

我正在使用 MobileFirst v8 和 Ionic v1.3.1 构建应用程序。当应用程序启动时,我的 app.js 文件

中有常规离子 angular 代码

这是app.js

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    ...
  })
.controller('IndexCtrl', function($scope, $state) {
    RememberMeService.checkIfLoggedIn().success(function(data) {
        alert("YAY!!");
    }).error(function(data) {
        alert('fail :(');
    });

});

function wlCommonInit() {
var serverUrl = WL.App.getServerUrl(function(success){
    console.log(success);
}, function(fail){
    console.log(fail);
});

WLAuthorizationManager.obtainAccessToken().then(
    function (accessToken) {
        console.log(">> Success - Connected to MobileFirst Server");       
    },
    function (error) {
        console.log(">> Failed to connect to MobileFirst Server");  
        console.log(error);        
    }
);

这是 RememberMeService

.service('RememberMeService', function($q) {
return {
    checkIfLoggedIn: function() {
        var deferred = $q.defer();
        var promise = deferred.promise;
        var securityCheckName = 'UserLogin';

        WLAuthorizationManager.obtainAccessToken(securityCheckName).then(
                function (accessToken) {
                    console.log('USER IS LOGGED IN!!!');
                },
                function (response) {
                    console.log("obtainAccessToken onFailure: " + JSON.stringify(response));
            }
        );

        promise.success = function(fn) {
            promise.then(fn);
            return promise;
        }
        promise.error = function(fn) {
            promise.then(null, fn);
            return promise;
        }

        return promise;

    }
}
})

当前发生的是 RememberMeService 在这一行 WLAuthorizationManager.obtainAccessToken(securityCheckName).then 处被调用失败,因为它说 WLAuthorizationManager 未定义。没有定义是因为app.js中的wlCommonInit()函数还没有完成,是Async函数。

那么我怎样才能延迟控制器或服务被调用,直到 wlCommonInit() 函数完成并且 WLAuthorizationManager 被定义?

感谢您的帮助

编辑

这是我的新 function wlCommonInit() {

function wlCommonInit() {
var serverUrl = WL.App.getServerUrl(function(success){
    console.log(success);
}, function(fail){
    console.log(fail);
});

WLAuthorizationManager.obtainAccessToken().then(
    function (accessToken) {
        console.log(">> Success - Connected to MobileFirst Server"); 
        angular.bootstrap(document.getElementById('indexBody'), ['app']);
    },
    function (error) {
        console.log(">> Failed to connect to MobileFirst Server");  
        console.log(error);        
    }
);

我的index.html是

<body id="indexBody">
    <h1>test</h1>
</body>

我收到错误

kError in Success callbackId: WLAuthorizationManagerPlugin887134242 : Error: [$injector:modulerr] Failed to instantiate module ng due to:
TypeError: Cannot set property 'aHrefSanitizationWhitelist' of null

我 bootstrap 做得对吗

您可以 bootstrap 在 wlCommonInit 成功回调中手动 angular 应用程序。有关详细说明,请参阅 https://docs.angularjs.org/guide/bootstrap#manual-initialization