简体   繁体   English

等待异步任务完成,然后再启动Angular JS

[英]Wait for Async Task to Finish Before starting Angular JS

I am using MobileFirst v8 and Ionic v1.3.1 to build an app. 我正在使用MobileFirst v8和Ionic v1.3.1来构建应用程序。 When the app starts I have the regular ionic angular code in my app.js file 应用启动时,我的app.js文件中包含常规离子角度代码

This is app.js 这是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);        
    }
);

This is the RememberMeService 这是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;

    }
}
})

What currently happens is the RememberMeService is called at fails at this line WLAuthorizationManager.obtainAccessToken(securityCheckName).then because it says WLAuthorizationManager is not defined. 当前发生的情况是,在此行WLAuthorizationManager.obtainAccessToken(securityCheckName).then处调用“ RememberMeService”失败。然后WLAuthorizationManager.obtainAccessToken(securityCheckName).then因为它说WLAuthorizationManager It is not defined because the wlCommonInit() function in app.js has not finished yet, it is the Async function. 未定义,因为app.jswlCommonInit()函数尚未完成,它是Async函数。

So how can I delay either the controller or the service from being called until, the wlCommonInit() function has finished and WLAuthorizationManager is defined? 那么,如何才能延迟调用控制器或服务,直到wlCommonInit()函数完成并且定义了WLAuthorizationManager

Thanks for the help 谢谢您的帮助

EDIT 编辑

Here is my new function wlCommonInit() { 这是我的新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);        
    }
);

my index.html is 我的index.html是

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

And I get error 我得到错误

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

am i doing the bootstrap correctly 我在正确地执行引导程序吗

You can bootstrap manually your angular App in wlCommonInit success callback. 您可以在wlCommonInit成功回调中手动引导您的角度应用程序。 See https://docs.angularjs.org/guide/bootstrap#manual-initialization for detailed explanations. 有关详细说明,请参见https://docs.angularjs.org/guide/bootstrap#manual-initialization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM