1

次のファクトリとコントローラーがあります。

(function () {
    'use strict';
    angular.module('app.core')
    .factory('Auth', ['$http', function AuthFactory($http) {

        return {
            NavAuth: function (Tab) {
                return $http({ method: 'GET', url: 'Dashboard/AuthorizeNavItem', params: { Name: Tab } });
            }
        }

    }]);
})();


angular
.module('myapp')
.controller('IndexController', ['fuseTheming', 'msNavigationService', 'Auth', function (fuseTheming, msNavigationService, Auth) {
    var vm = this;

    //Define the tabs
    msNavigationService.saveItem('app', {
        title: 'QPSTool',
        group: true,
        weight: 1
    });

    msNavigationService.saveItem('app.dashboard', {
        title: 'Dashboard',
        icon: 'icon-tile-four',
        state: 'app.dashboard',
        weight: 1
    });

    Auth.NavAuth('IT').success(function (result) {
        if (result == 'Authorized') {
            msNavigationService.saveItem('app.it', {
                title: 'IT',
                icon: 'icon-monitor',
                weight: 2
            });
        }
    });

    Auth.NavAuth('Users').success(function (result) {
        if (result == 'Authorized') {
            msNavigationService.saveItem('app.it.users', {
                title: 'Users',
                state: 'app.it.users',
                weight: 1
            });
        }
    });

    Auth.NavAuth('Admin').success(function (result) {
        if (result == 'Authorized') {
            msNavigationService.saveItem('app.admin', {
                title: 'Admin',
                icon: 'icon-radioactive',
                weight: 3
            });
        }
    });

    Auth.NavAuth('NavControl').success(function (result) {
        if (result == 'Authorized') {
            msNavigationService.saveItem('app.admin.navcontrol', {
                title: 'Navigation Auth',
                state: 'app.admin.navcontrol',
                weight: 1
            });
        }
    });

    // Data
    vm.themes = fuseTheming.themes;
}]);

ファクトリ メソッド NavAuth が行うことは、パラメーターとしてナビゲーション項目名を取り、ユーザーがこの項目へのアクセスを許可されているかどうかを通知することです。

私が抱えている問題は、msNavigationService.saveItemデータを使用するとコントローラーでランダムな順序で返されることです。NavControlそのため、以前の承認済みを返しITます。

これにより、サイド ナビゲーションが正しくレンダリングされません。

コントローラーで指定した順序で物事が実行されるようにするにはどうすればよいですか (つまり、一方が完了するまでもう一方を待機させるにはどうすればよいですか)。

4

2 に答える 2

1

あなたの問題は、約束$q.allの一部を置くことでは解決されないと思うthenので、次のようにするでしょう

NavAuth('IT').then(function (res) {
  // doWhatever IT function does;
  ...
  NavAuth('NavControl').then(function (res) {
    // doWhatever NavControl function does;
    ...
  })
})

プロミスの を使用thenすると、コードを順番に実行するように強制$q.all()できます。 に渡すすべてのプロミス$q.all()が完了するまで、すべてを実行することはできません。それはあなたが望むものではありません。

于 2016-11-18T16:46:40.267 に答える