根据用户选择AngularJS构建URL



我正在构建一个网站,该网站基本上在表格中显示来自JSON文件的特定数据。

我的问题是,我将有超过 250 个潜在的不同 JSON 文件,可以根据用户的选择加载这些文件。例如,如果用户单击按钮"a",然后单击按钮"b",则需要加载文件"a_b.json"。现在我已经用一个 json 文件进行了测试,这是我的测试代码:

var app = angular.module('webStatistics', ['ngRoute']);
app.config(function($routeProvider){
    $routeProvider
        .when('/stats/views/', {templateUrl: 'home'})
        .when('/stats/views/home.html', {
            controller : 'testController'
            templateUrl: '/stats/views/home.html'
        })
        .otherwise({redirectTo : '/'});
});
app.controller('testController', function($scope, $http) {
      $http.get('../app/sources/stats/004/a_b.json').success(function(data) {
        $scope.variables = data;
      });
});

想知道是否有办法"动态构造"我提供给$http服务的 URL?因此,如果用户的选择是x和选项z->.。/stats/x/x_z.json

一次只能加载一个文件。

希望这有帮助。

var app = angular.module('webStatistics', ['ngRoute']);
app.config(function($routeProvider){
    $routeProvider
        .when('/stats/views/', {templateUrl: 'home'})
        .when('/stats/views/home.html', {
            controller : 'testController'
            templateUrl: '/stats/views/home.html'
        })
        .otherwise({redirectTo : '/'});
});
app.controller('testController', function($scope, $http) {
    $scope.item1 = '';
    $scope.item2 = '';
    $scope.getData() {
        if ($scope.item1 && $scope.item2) {
            var api = '../app/sources/stats/004/' + item1 + '_' + item2 + '.json';
            $http.get(api).success(function(data) {
                $scope.variables = data;
            });
        }
    };
});

最新更新