Javascript 具有不同url的Angular.js动态模板
我试图构建一个带有静态页眉和页脚布局的页面,但使用ng view或ng include根据URL更改容器模板。我尝试了两种方法: 使用$routeProvider:Javascript 具有不同url的Angular.js动态模板,javascript,angularjs,Javascript,Angularjs,我试图构建一个带有静态页眉和页脚布局的页面,但使用ng view或ng include根据URL更改容器模板。我尝试了两种方法: 使用$routeProvider: app.config(['$routeProvider', function ($routeProvider) { $routeProvider.when('/', { template: 'views/home.html', controller: 'Ctrl', }); $
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/', {
template: 'views/home.html',
controller: 'Ctrl',
});
$routeProvider.when('/contact-us', {
template: 'views/contact.html',
controller: 'Ctrl',
});
$routeProvider.otherwise({
redirectTo: '/'
});
}]);
或用于返回模板URL的控制器函数:
app.controller('locationCtrl', function ($scope, $location, $routeParams) {
$scope.templateUrl = function() {
return "/views/home.html";
}
});
但是,无论是哪种情况,我都无法收到“联系我们”错误消息
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/', {
template: 'views/home.html',
controller: 'Ctrl',
});
$routeProvider.when('/contact-us', {
template: 'views/contact.html',
controller: 'Ctrl', // Controller should be different
});
$routeProvider.otherwise({
redirectTo: '/'
});
}]);
替换为:
app.config(['$routeProvider', function ($routeProvider)
$routeProvider.when('/', {
templateUrl: 'views/home.html', //template should be templateUrl
controller: 'Ctrl',
});
$routeProvider.when('/contact-us', {
templateUrl: 'views/contact.html', //template should be templateUrl
controller: 'Ctrl',
});
$routeProvider.otherwise({
redirectTo: '/'
});
}]);
你的第一种方法会奏效
替换为:
app.config(['$routeProvider', function ($routeProvider)
$routeProvider.when('/', {
templateUrl: 'views/home.html', //template should be templateUrl
controller: 'Ctrl',
});
$routeProvider.when('/contact-us', {
templateUrl: 'views/contact.html', //template should be templateUrl
controller: 'Ctrl',
});
$routeProvider.otherwise({
redirectTo: '/'
});
}]);
您的第一种方法将起作用。routerProvider在所有控制器之前运行。因此,您不能简单地动态更改TemplateURL。但您可以有条件地使用:
<div ng-controller="SubPageCtrl>
<ng-include src="templateUrl"></ng-include>
</div>
<script>
function SubPageCtrl($scope) {
if ( something ) {
$scope.templateUrl = '/some.html';
} else {
$scope.templateUrl = '/other.html';
}
}
</script>
routerProvider在所有控制器之前运行。因此,您不能简单地动态更改TemplateURL。但您可以有条件地使用:
<div ng-controller="SubPageCtrl>
<ng-include src="templateUrl"></ng-include>
</div>
<script>
function SubPageCtrl($scope) {
if ( something ) {
$scope.templateUrl = '/some.html';
} else {
$scope.templateUrl = '/other.html';
}
}
</script>
您应该定义从更具体的URL到更通用的URL的路由。因为路由与以您的定义开头的URL匹配
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/contact-us', {
template: 'views/contact.html',
controller: 'Ctrl',
});
$routeProvider.when('/', {
template: 'views/home.html',
controller: 'Ctrl',
});
$routeProvider.otherwise({
redirectTo: '/'
});
}]);
您应该定义从更具体的URL到更一般的URL的路由。因为路由与以您的定义开头的URL匹配
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/contact-us', {
template: 'views/contact.html',
controller: 'Ctrl',
});
$routeProvider.when('/', {
template: 'views/home.html',
controller: 'Ctrl',
});
$routeProvider.otherwise({
redirectTo: '/'
});
}]);
所以我有一个www.domain.com,这将加载我的home.html。www.domain.com/contact-us将加载我的contact.htmlw当用户关闭浏览器w/JavaScript时会发生什么?因此我有一个www.domain.com,这将加载我的home.html。并且www.domain.com/contact-us将加载我的contact.htmlw当用户关闭w/JavaScript浏览时会发生什么?谢谢,它在home.html上工作,但在与我们联系时,它似乎会不断重新加载页面。我建议不要为根本不同的路由回收控制器。你不会用MVC来做这件事,所以尽管有可能,但用Angular是不可取的。是的,我做了,但它只是不断地重新加载。对不起,我不知道为什么会这样。$routeProvider.when('/contact-us',{templateUrl:'views/contact.html',});这里包括控制器是的,我已经尝试过了,但是索引页面仍然保持呈现(重新加载?)谢谢它在home.html上工作,但是当与我们联系时,它似乎保持重新加载页面。我建议不要为基本上不同的路由回收控制器。你不会用MVC来做这件事,所以尽管有可能,但用Angular是不可取的。是的,我做了,但它只是不断地重新加载。对不起,我不知道为什么会这样。$routeProvider.when('/contact-us',{templateUrl:'views/contact.html',});这里包括控制器是的,我已经试过了,但是索引页仍然保持呈现(重新加载?)