gpt4 book ai didi

angularjs - Angular 动态 templateUrl

转载 作者:行者123 更新时间:2023-12-03 00:02:31 25 4
gpt4 key购买 nike

您好,还有一个问题,我有下一个 Angular 配置:

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'});
}];

而不是“???”我需要放置动态网址,尝试在 Controller 中执行此操作,例如:

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;
});

}

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

最佳答案

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

我认为完成您想要做的事情的最简单或最干净的方法就是在路由定义中使用内联 Controller 。我设置了一个简单的 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'});
}];

关于angularjs - Angular 动态 templateUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16316087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com