javascript - Angular v1+ : loading different area in page by angularjs

标签 javascript angularjs

假设当第一次加载网站时我需要加载左侧和顶部菜单。

两个菜单将独立加载,因此任何人都可以先加载并显示。 所以告诉我如果首先加载顶部或左侧菜单,我需要应用的技巧来同时显示左侧和顶部菜单。一些我需要同时显示两个菜单的方法。

告诉我下面需要更改的代码。以下代码只是示例代码,未经测试。

app.service("TopMenuService", function ($http) {  
    this.getTopMenu = function () {  
        debugger;  
        return $http.get("/employee/getTopMenu");  
    };  
});  

app.service("LeftMenuService", function ($http) {  
    this.getLeftMenu = function () {  
        debugger;  
        return $http.get("/employee/getLeftMenu");  
    };  
});  

app.controller("EmpCtrl", function ($scope, TopMenuService,LeftMenuService) {  

    GetTopMenu();  
    GetLeftMenu();  

    function GetTopMenu() {  

        debugger;  
        var _getTopMenu = EmployeeService.getTopMenu();  
        _getTopMenu.then(function (topmenu) {  
            $scope.topmenu = topmenu.data;  
        }, function () {  
            alert('Data not found');  
        });  
    }  

    function GetLeftMenu() {  

        debugger;  
        var _getLeftMenu = EmployeeService.getLeftMenu();  
        _getLeftMenu.then(function (leftmenu) {  
            $scope.leftmenu = leftmenu.data;  
        }, function () {  
            alert('Data not found');  
        });  
    }      
});  

最佳答案

由 promise 控制的加载菜单过程怎么样?

Note on $q.all() Promises in AngularJs are handled by the $q service. Promises are used to synchronize the execution of tasks on concurrent envirorments, AngularJs's $q.all receives a list of various promises and fires the then callback when all promises on the list gets resolved, in this case, the two promises are the $http.get() of each menu, it's an async promise case so that when the response is sent, it resolves the promise and fires the then() registered callback, eventually will fire $q.all() as well.

app.controller("EmpCtrl", function ($scope, $q, TopMenuService, LeftMenuService) {

    $scope.shouldDisplayMenus = false;

    LoadMenus().then(function () {
        $scope.shouldDisplayMenus = true;
    });

    function LoadMenus() {
        return $q.all([
            GetTopMenu(),  
            GetLeftMenu()
        ]);
    }

    function GetTopMenu() {  

        debugger;  
        var _getTopMenu = EmployeeService.getTopMenu();  
        _getTopMenu.then(function (topmenu) {  
            $scope.topmenu = topmenu.data;  
        }, function () {  
            alert('Data not found');  
        });

        return _getTopMenu;
    }  

    function GetLeftMenu() {  

        debugger;  
        var _getLeftMenu = EmployeeService.getLeftMenu();  
        _getLeftMenu.then(function (leftmenu) {  
            $scope.leftmenu = leftmenu.data;  
        }, function () {  
            alert('Data not found');  
        });

        return _getLeftMenu;
    }      
}); 

关于javascript - Angular v1+ : loading different area in page by angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43637755/

相关文章:

javascript - 将参数传递给 array.prototype.findIndex(callback[, thisArg])

javascript - 迭代添加addEventListener

javascript - 如何关闭 mariadb 中的自动提交?

javascript - angularjs http post 请求中的数据在哪里?

angularjs - 使用 AngularJs 的可排序表格列

javascript - 调用 $provide 外部配置。 Angular

javascript - 如何在 ng-Table 中隐藏页面切片?

javascript - 减去10的最大倍数

javascript - 使用字符串访问 JSON 数据

javascript - 在模态对话框/框中显示搜索结果