AngularJS,如何从 returns json 的 Web 服务动态生成路由规则?
AngularJS, how to generate routing rules dynamically from web service which returns json?
我想根据 returns 一个 JSON 文件的 Web 服务动态生成路由规则。
这可能吗?
我正在使用 Angular 1.4.7.
我找到了一个很酷的方法,
解决方案是写一个全局变量然后更新内容。
这是一个例子:
//global variable
var $routeProviderReference;
//global variable
var app=angular.module('MyApp',['ngMaterial', 'ngMessages', 'ngRoute', 'ngSanitize']);
app.config(function($mdThemingProvider, $routeProvider, $locationProvider) {
//link global variable to router Provider reference
$routeProviderReference = $routeProvider;
//link global variable to router Provider reference
});
app.run(function($templateCache, $rootScope, $http, $route, dataFactory) {
//get routing rules from web service
$http({ method: 'GET', url: 'webservice-URL' }).then(function (e){
//set routing rules in global variable
$routeProviderReference.when('/'+e.data.page, {
templateUrl: 'static/tpl/default.html',
controller: function($scope) {
$scope.data=e.data.feed.entry;
}
});
}, function (err){
console.log(err);
);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
我想根据 returns 一个 JSON 文件的 Web 服务动态生成路由规则。 这可能吗? 我正在使用 Angular 1.4.7.
我找到了一个很酷的方法, 解决方案是写一个全局变量然后更新内容。 这是一个例子:
//global variable
var $routeProviderReference;
//global variable
var app=angular.module('MyApp',['ngMaterial', 'ngMessages', 'ngRoute', 'ngSanitize']);
app.config(function($mdThemingProvider, $routeProvider, $locationProvider) {
//link global variable to router Provider reference
$routeProviderReference = $routeProvider;
//link global variable to router Provider reference
});
app.run(function($templateCache, $rootScope, $http, $route, dataFactory) {
//get routing rules from web service
$http({ method: 'GET', url: 'webservice-URL' }).then(function (e){
//set routing rules in global variable
$routeProviderReference.when('/'+e.data.page, {
templateUrl: 'static/tpl/default.html',
controller: function($scope) {
$scope.data=e.data.feed.entry;
}
});
}, function (err){
console.log(err);
);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>