Angularjs:路线和控制器,而templateURLs? [英] Angularjs: routes and controllers without templateURLs?
问题描述
是否有可能使用Angularjs的路由和控制,而不templateURL?
Is it possible to use Angularjs's routing and controller without the templateURL?
例如,下面是我目前的路线,控制器和模板的URL,
For instance, below is my current routes, controllers, and template urls,
return app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when("/",
{
templateUrl: "js/template/1.html",
controller: "controller1"
})
.when("/list1",
{
templateUrl: "js/template/2.html",
controller: "controller2"
})
...
和我有 NG-视图
在我的html,
And I have ng-view
in my html,
<div ng-view></div>
我的路线测试没有 templateUrl
,
return app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when("/",
{
//templateUrl: "js/template/1.html",
controller: "controller1"
})
.when("/view1",
{
//templateUrl: "js/template/2.html",
controller: "controller2"
})
...
结果 - 没有什么是我的显示屏幕上。显然,控制器不能再触发。
the result - nothing is displayed on my screen. obviously the controller cannot be triggered anymore.
推荐答案
角使用$ routeProvider定义到一个控制器连接到您的视图。如果你不想指定$ routeProvider配置的细节,另一种选择是使用像让一个页面上的应用程序的土地 -
Angular uses the $routeProvider Definition to attach a controller to your view. If you don't want to specify the details in $routeProvider config, other option is to let the application land on one page using something like -
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html'
})
.when()
.otherwise({
redirectTo: '/'
});
});
这样用户将降落在main.html中。现在,在main.html中,你可以有多种不同的HTML模板一样 -
This way user will land on main.html. Now on the main.html you can include several different html templates like -
<div ng-include src="'js/template/1.html'" ng-controller = 'Controller1'></div>
<div ng-include src="'js/template/2.html'" ng-controller = 'Controller2'></div>
注意使用/模板,包括上面的语法 - '&LT;&LT;路径&GT;&GT;'
希望这有助于。
这篇关于Angularjs:路线和控制器,而templateURLs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!