javascript 如何在 angularjs 中加载不同的布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17273671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load a different layout in angularjs
提问by Aamir Shah
I have 2 layout files. Layout-1 Layout-2
我有 2 个布局文件。布局 1 布局 2
The route /one
is the starting point which loads the Layout-1
, but now if I click on a link /two
it gets me the file, but with Layout-1
, instead i want, if i hit /two
, Layout-2
should be loaded.
路线/one
是加载 的起点Layout-1
,但是现在如果我单击链接,/two
它会为我获取文件,但是Layout-1
如果我点击/two
,则Layout-2
应该加载,而不是我想要的。
If i refresh the page, i get the correct thing coz that time i hit the server directly. So is there any way from angular to specify which layout file to load from Server.
如果我刷新页面,我会得到正确的东西,因为我直接访问了服务器。那么有没有办法从 angular 指定从 Server.load 加载哪个布局文件。
Thanks.
谢谢。
采纳答案by Aamir Shah
回答by McLosys Creative
Loading new layout template means loading new html embedded with new javascript and css files.
This is equal to redirecting page to new location. You can use $window.location.href="new/layout/template"
. Read angularjs developer guidefor more info.
加载新的布局模板意味着加载嵌入了新的 javascript 和 css 文件的新 html。这相当于将页面重定向到新位置。您可以使用 $window.location.href="new/layout/template"
. 阅读angularjs 开发者指南以获取更多信息。
回答by SET
If it stands for client side routing then you should use $routeProviderto write a route for this
如果它代表客户端路由,那么你应该使用$routeProvider为此编写路由
var app = angular.module('app', []);
app.config(function($routeProvider){
$routeProvider
.when('/one', {templateUrl: 'tmpl1.html'})
.when('/two', {templateUrl: 'tmpl2.html'})
.otherwise({redirectTo: '/one'});
});
It will allow you to define separate template and controller for another url/action
它将允许您为另一个 url/action 定义单独的模板和控制器
HereI made a working example of how it work.
在这里,我做了一个工作示例来说明它是如何工作的。
If it is about serverside routing then read manualand use it like this:
如果是关于服务器端路由,请阅读手册并像这样使用它:
match 'two' => 'controller#action'