0
我嘗試使用angularjs 這裏創建動態的框架是我的計劃......用戶必須添加從JSON文件中的新templateUrl和控制器這樣templates.json動態路由和控制器納克級
{
"pages" : [
{
"name" : "home",
"tempUrls" : "views/home",
"controller" : "HomeController"
},
{
"name" : "about",
"tempUrls" : "views/about",
"controller" : "AboutController"
},
{
"name" : "contact",
"tempUrls" : "views/contact",
"controller" : "ContactController"
}
]
}
從這裏我們的工作就是創造下襬像這樣hours.js在angularjs控制器和templateUrl使用頁名稱
var hours = angular.module('hours', ['ngRoute']);
var $routeProviderReference;
var currentRoute;
hours.config(function($routeProvider){
$routeProviderReference = $routeProvider;
})
.run(['$route', '$http', '$rootScope', function($route, $http, $rootScope){
$http.get("templates.json").success(function(data){
var loop = 0, currentRoute;
for(loop = 0; loop < data.pages.length; loop++){
currentRoute = data.pages[loop];
var routeName = "/" + currentRoute.name;
$routeProviderReference.when(routeName, {
templateUrl: currentRoute.tempUrls,
controller : currentRoute.controller,
resolve: {
param: function()
{
return currentRoute.resolve;
}
}
});
}
$route.reload();
});
}]);
hours.controller(currentRoute.controller, function($scope){
$scope.pageClass = 'page-' + currentRoute.name;
});
這裏是的index.html
<div ng-class="{{pageClass}}" ng-view></div>
,這裏是從angularJS靜態版本之前,我轉換爲動態
var hours = angular.module('hours', ['ngRoute']);
hours.config(function($routeProvider){
$routeProvider
.when('/', {
templateUrl: 'views/page-home.html',
controller: 'homeController'
})
});
hours.controller('homeController', function($scope){
$scope.pageClass = 'page-home';
});
我真的希望有人能夠幫助!
角的〔毫微級(https://docs.angularjs.org/api/ng/directive/ngClass )文件。此外,他可以使用'ng-class =「pageClass」' - 這可能會有所幫助:) – Sean3z
謝謝@ Sean3z,正在編輯您發佈的內容,並且您完全正確。事實上,自從我自己回顧文檔已經有一段時間了。 –