繁体   English   中英

如何使用角度JS中的$ Routeparams从json文件中检索数据

[英]How to retrieve data from a json file using $Routeparams in angular JS

在此输入图像描述

我正面临使用$ routeparams检索数据的问题,我将解决我的问题

/ 重新传输URL以重定向 /

  console.log($routeParams);
  console.log($routeParams.json_url);

    $.getJSON("./api-data/"+$routeParams.json_url, function(json){
        $scope.data = json;
         console.log('JSON--',$scope.data);
       $scope.processdata();
  });

/ 重新传输URL以重定向 /

这是我试图获取数据的方式,但是当我控制它的$ routeparams有数据 - >它有需要的URL但当我与“json url”连接时它显示“无法加载资源:服务器响应了一个控制台中404(未找到)状态和“未定义”状态

我正在分享我的app.js.

app.js

App.config(['$routeProvider',function($routeProvider, $routeParams) {

            $routeProvider

    .when('/base-product/:json_url?', {
                    templateUrl :'templates/base_product.html',
                    controller  :'BaseProductController'
                })      

我的menu.html

这是我点击此菜单中的特定链接时的菜单,它应该动态地重定向到关联的json文件

<ul id="submenu-2" class="collapse" >
                            <span ng-repeat="item in itemDetails">
                            <li><a href="#base-product?{{item.path}}" > {{item.title}}</a></li>
                            </span>

                        </ul>

我的action.json会是这样的

[

   {
      "title":"View",
      "path":"actions/view.json",
      "urlpath":"view?segment=view",
      "apiPath":"api/view",
      "methodType":"post"

   },

    {
       "title":"Add",
      "path":"actions/add.json",
      "urlpath":"view?segment=add",
      "apiPath":"api/add",
      "methodType":"post"

   },

]

我写了一个工厂来从json文件中获取数组值(action.json)

App.factory('itemsFactory', ['$http', function($http){
  var itemsFactory ={
    itemDetails: function() {
      return $http(
      {
        url: "api-data/action.json",
        method: "GET",
      })
      .then(function (response) {
        return response.data;
        });
      }
    };
    return itemsFactory;

}]);


App.controller('SidenavItems_controller', ['$scope', 'itemsFactory', function($scope, itemsFactory){

    console.log("Loading json array name is working fine and tested in console")
  var promise = itemsFactory.itemDetails();

    promise.then(function (data) {
        $scope.itemDetails = data;
        console.log(data);
    });
    $scope.select = function(item) {
      $scope.selected = item;
    }
    $scope.selected = {};
}]);

提前帮助我..谢谢你

您的网址参数错误,您不需要使用'?' 标记。 将其更改为

视图

<li><a href="#base-product/{{item.path}}" > {{item.title}}</a></li>

JS

.when('/base-product/:json_url', {...}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM