2

嗨还有一个问题,我有下一个角度配置:

angular.module('ow', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
    when('/:placeId', {templateUrl: 'partials/menu.html',   controller: MenuCtrl}).
    when('/menu/:itemId', {templateUrl: 'partials/menu-details.html', controller: MenuItemCtrl}).
    when('/look/refill', {templateUrl: 'partials/refill.html', controller: RefillCtrl}).
    when('/look/orderCart', {templateUrl: 'partials/orderCart.html', controller: OrderCartCtrl}).
    when('/lang/:lang', {templateUrl: 'partials/menu.html', controller: LangCtrl}).
    when('/waiter/:redirect', {templateUrl: "???????", controller: WaiterCtrl}).
    otherwise({redirectTo: '/0'});
}];

代替 ”?????” 我需要放置动态网址,尝试在控制器中执行此操作,例如:

function WaiterCtrl($routeParams, $location, sharedData, $http, $route) {
$http.get(config.urls.ajaxWaiter + "{\"p\":\"" + sharedData.getOrderCart().orderPlace + "\"}").success(function(dataDetails) {
    if ($routeParams.redirect == "menu") {
        $route.templateUrl = "partials/menu.html";
        $location.path("/");
    }
    if ($routeParams.redirect == "menuDetails") {
        $route.templateUrl = "partials/menu-details.html";
        $location.path("/menu/" + sharedData.getMenu());
    }
    if ($routeParams.redirect == "orderCart") {
        $route.templateUrl = "partials/orderCart.html";
        $location.path("/orderCart");
    }
    if ($routeParams.redirect == "refill") {
        $route.templateUrl = "partials/refill.html";
        $location.path("/refill");
    }
    return $route.templateUrl;
});

}

但它不起作用......你能帮帮我吗?

4

1 回答 1

6

您将无法将动态代码放入路由中。这是因为路由发生在 Config 阶段,该阶段在 Angular 开始运行您的应用程序之前执行。

我认为做你想做的最简单或最干净的方法就是在路由定义中有一个内联控制器。我设置了一个简单的 plunk 来显示路由定义中的重定向:http ://plnkr.co/edit/aeSjmn?p=preview

以下是一些可能对您有用的示例代码:

angular.module('ow', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
    when('/:placeId', {templateUrl: 'partials/menu.html',   controller: MenuCtrl}).
    when('/menu/:itemId', {templateUrl: 'partials/menu-details.html', controller: MenuItemCtrl}).
    when('/look/refill', {templateUrl: 'partials/refill.html', controller: RefillCtrl}).
    when('/look/orderCart', {templateUrl: 'partials/orderCart.html', controller: OrderCartCtrl}).
    when('/lang/:lang', {templateUrl: 'partials/menu.html', controller: LangCtrl}).
    when('/waiter/:redirect', {template: '', controller: function ($scope, $routeParams, $location) {
      function WaiterCtrl($routeParams, $location, sharedData, $http, $route) {
        $http.get(config.urls.ajaxWaiter + "{\"p\":\"" + sharedData.getOrderCart().orderPlace + "\"}").success(function(dataDetails) {
            if ($routeParams.redirect == "menu") 
              $location.path("/");
            else
              $location.path("/" + $routeParams.redirect);
        })
    }}).
    otherwise({redirectTo: '/0'});
}];
于 2013-05-01T13:25:18.550 回答