我如何在路由提供程序中存储和使用 json 数据
How can i store and use the json data inside route provider
$routeProvider
.when('/accordion', {(need to get the name from json)
templateUrl: 'Samples/accordion.html',(need to get the urlfrom json)
controller: "AccordionCtrl",
})
如何在 json
中存储名称和模板 url 以及如何从 json
中获取名称和模板 url
请检查下面的代码,它将输入作为 templates.json
文件,这将有助于创建动态路由
'use strict';
var app = angular.module('app', ['ngRoute']);
var $routeProviderReference;
var currentRoute;
app.config(function($routeProvider){
$routeProviderReference = $routeProvider;
});
app.service('shareData', function(){
app.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();
});
}]);
app.controller(currentRoute.controller, function($scope){
$scope.pageClass = 'page-' + currentRoute.name;
});
});
同时找到 json 文件演示如下:
{
"pages" : [
{
"name" : "home",
"tempUrls" : "views/home",
"controller" : "HomeController"
},
{
"name" : "about",
"tempUrls" : "views/about",
"controller" : "AboutController"
},
{
"name" : "contact",
"tempUrls" : "views/contact",
"controller" : "ContactController"
}
]
}
希望对您有所帮助!
$routeProvider
.when('/accordion', {(need to get the name from json)
templateUrl: 'Samples/accordion.html',(need to get the urlfrom json)
controller: "AccordionCtrl",
})
如何在 json
中存储名称和模板 url 以及如何从 json
请检查下面的代码,它将输入作为 templates.json
文件,这将有助于创建动态路由
'use strict';
var app = angular.module('app', ['ngRoute']);
var $routeProviderReference;
var currentRoute;
app.config(function($routeProvider){
$routeProviderReference = $routeProvider;
});
app.service('shareData', function(){
app.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();
});
}]);
app.controller(currentRoute.controller, function($scope){
$scope.pageClass = 'page-' + currentRoute.name;
});
});
同时找到 json 文件演示如下:
{
"pages" : [
{
"name" : "home",
"tempUrls" : "views/home",
"controller" : "HomeController"
},
{
"name" : "about",
"tempUrls" : "views/about",
"controller" : "AboutController"
},
{
"name" : "contact",
"tempUrls" : "views/contact",
"controller" : "ContactController"
}
]
}
希望对您有所帮助!