我是Angular的新手,我想知道如果ngRoute等于什么,是否有任何方法来渲染脚本(CSS / JS)。例如,我定义了以下路线:
.when('/', {
templateUrl : 'views/index.html',
controller : 'ProductsController'
})
// route for the contactus page
.when('/admin', {
templateUrl : 'admin/index.html',
controller : 'AdminController',
})
并且当路线为<link href="css/app.css" rel="stylesheet">
时,我希望将"/"
呈现在头部中;当路线为<link href="css/admin.css" rel="stylesheet">
时,我想呈现"/admin"
。
答案 0 :(得分:0)
我刚刚找到解决方案,不知道它是否合适,因为它在</head>
之后呈现样式表,但这解决了我的问题。我所做的是,我将MainController
定义为:
myApp.controller('MainController', ['$scope','$location', '$routeParams', function($scope, $location, $routeParams) {
if($location.path() == '/admin') {
$scope.isAdmin = true;
}
else {
$scope.isAdmin = false;
}
}]);
之后,我没有在</head>
内放置样式表,而是在主题后面放置一个指令,并在头部后面包装样式表:
<div ng-controller="MainController">
<link href="css/app.css" rel="stylesheet" ng-if="!isAdmin">
</div>
答案 1 :(得分:0)
在<head>
你可以放:
<link rel="stylesheet" ng-href={{selectedType}}>
在主控制器中,您可以将条件设为isAdmin
$rootScope.selectedType = isAdmin ? "css/admin.css" : "css/app.css"
确保{h} ng-app
<html ng-app="myApp">