如何在路由提供程序中存储和使用json数据

时间:2022-05-12 20:36:25
$routeProvider

          .when('/accordion', {(need to get the name from json)

       templateUrl: 'Samples/accordion.html',(need to get the urlfrom json)
controller: "AccordionCtrl",

          })

How to store the name and template url in json and how to get the name and template url from json

如何在json中存储名称和模板URL以及如何从json获取名称和模板URL

1 个解决方案

#1


1  

Please check below code which will take input as templates.json file and it will help create dynamic routes

请检查下面的代码,它将输入作为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;
    });
});

Also find the json file demo as below:

另外找到json文件演示如下:

{
  "pages" : [
    {
    "name"        : "home",
    "tempUrls"    : "views/home",
    "controller"  : "HomeController"
    },
    {
      "name"        : "about",
      "tempUrls"    : "views/about",
      "controller"  : "AboutController"
    },
    {
      "name"        : "contact",
      "tempUrls"    : "views/contact",
      "controller"  : "ContactController"
    }
  ]
}

Hope this would help you out.!

希望这会帮助你。

#1


1  

Please check below code which will take input as templates.json file and it will help create dynamic routes

请检查下面的代码,它将输入作为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;
    });
});

Also find the json file demo as below:

另外找到json文件演示如下:

{
  "pages" : [
    {
    "name"        : "home",
    "tempUrls"    : "views/home",
    "controller"  : "HomeController"
    },
    {
      "name"        : "about",
      "tempUrls"    : "views/about",
      "controller"  : "AboutController"
    },
    {
      "name"        : "contact",
      "tempUrls"    : "views/contact",
      "controller"  : "ContactController"
    }
  ]
}

Hope this would help you out.!

希望这会帮助你。