在基于一个布尔值的 angularJS 路由

发布时间: 2017/4/16 3:28:18
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
.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"}
                        );
                    }]
                }
            })
        ;
    }])

基本上我想要加载基于一个布尔值的另一个部分。有人可以指导我通过这吗?很新的角 !

解决方法 1:

试试这个,可能工作︰

.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"}
                            );
                        }
                    }]
                }
            } 
        ;
    }])
赞助商