基于布尔值的 angularJS 中的路由

Routing in angularJS based on a boolean value

本文关键字:路由 angularJS 于布尔      更新时间:2024-03-29
.config(["$routeProvider", 'partial', 'contentUrl', 'appContext', function ($routeProvider, partial, contentUrl, appContext) {
        $routeProvider
            .when('/notifications', {
                templateUrl: partial('popup.html'),
                controller: 'popUpCtrl',
                resolve: {
                    notifications: ['$http', function($http) {
                        return $http.post(
                            appContext('ViewAllNotifications.json'),
                            {"categoryGroupType":"ROLB","isArchived":"N","channelTypeCode":"101","limit":"20","page":"0","customerType":"A"}
                        );
                    }]
                }
            })
        ;
    }])

上面的代码工作得很好。我想在这里做一些基于条件的路由。我可以做类似的事情吗?

.config(["$routeProvider", 'partial', 'contentUrl', 'appContext', function ($routeProvider, partial, contentUrl, appContext) {
        $routeProvider
            .when('/notifications',
                if(something){
                templateUrl: partial('popup.html'),
                controller: 'popUpCtrl',
                resolve: {
                    notifications: ['$http', function($http) {
                        return $http.post(
                            appContext('ViewAllNotifications.json'),
                            {"categoryGroupType":"ROLB","isArchived":"N","channelTypeCode":"101","limit":"20","page":"0","customerType":"A"}
                        );
                    }]
                }
            } 
            else{
                templateUrl: partial('popup2.html'),
                controller: 'popUpCtrl',
                resolve: {
                    notifications: ['$http', function($http) {
                        return $http.post(
                            appContext('ViewAllNotifications2.json'),
                            {"categoryGroupType":"ROLB","isArchived":"N","channelTypeCode":"101","limit":"20","page":"0","customerType":"A"}
                        );
                    }]
                }
            })
        ;
    }])

基本上我想根据布尔值加载另一个部分。有人可以指导我完成这个吗?我对棱角很陌生。

试试这个,可能会起作用:

.config(["$routeProvider", 'partial', 'contentUrl', 'appContext', function ($routeProvider, partial, contentUrl, appContext) {
        $routeProvider
            .when('/notifications', {
                templateUrl: function (){
                    if (something) {
                        return partial('popup.html');
                    }
                    else {
                        return partial('popup2.html');
                    }
                },
                controller: 'popUpCtrl',
                resolve: {
                    notifications: ['$http', function($http) {
                        if (something) {
                            return $http.post(
                                appContext('ViewAllNotifications.json'),
                                {"categoryGroupType":"ROLB","isArchived":"N","channelTypeCode":"101","limit":"20","page":"0","customerType":"A"}
                            );
                        }
                        else {
                            return $http.post(
                                appContext('ViewAllNotifications2.json'),
                                {"categoryGroupType":"ROLB","isArchived":"N","channelTypeCode":"101","limit":"20","page":"0","customerType":"A"}
                            );
                        }
                    }]
                }
            } 
        ;
    }])