繁体   English   中英

使用$ http从本地json文件获取值的另一种方法

[英]Alternative way to fetch value from a local json file using $http

我想从本地存储的国家json文件加载国家列表。 我将文件包含在index.html为:

<!-- Including Json -->
<script src="json/countries.json"></script>

Json文件保存在名称为countries.json的json文件夹中,文件内容为:

[  
   {  
      "country_id":"1",
      "country_name":"Afghanistan"
   },
   {  
      "country_id":"2",
      "country_name":"Albania"
   },
   {  
      "country_id":"3",
      "country_name":"Algeria"
   },
   {  
      "country_id":"4",
      "country_name":"American Samoa"
   },
   {  
      "country_id":"5",
      "country_name":"Andorra"
   },
   {  
      "country_id":"6",
      "country_name":"Angola"
   },
   {  
      "country_id":"7",
      "country_name":"Anguilla"
   },
   {  
      "country_id":"8",
      "country_name":"Antarctica"
   },
   {  
      "country_id":"9",
      "country_name":"Antigua and Barbuda"
   },
   {  
      "country_id":"10",
      "country_name":"Argentina"
   },
   {  
      "country_id":"11",
      "country_name":"Armenia"
   },
   {  
      "country_id":"12",
      "country_name":"Aruba"
   },
   {  
      "country_id":"13",
      "country_name":"Australia"
   },
   {  
      "country_id":"14",
      "country_name":"Austria"
   },
   {  
      "country_id":"15",
      "country_name":"Azerbaijan"
   },
   {  
      "country_id":"16",
      "country_name":"Bahamas"
   },
   {  
      "country_id":"17",
      "country_name":"Bahrain"
   },
   {  
      "country_id":"18",
      "country_name":"Bangladesh"
   },
   {  
      "country_id":"19",
      "country_name":"Barbados"
   },
   {  
      "country_id":"20",
      "country_name":"Belarus"
   },
   {  
      "country_id":"21",
      "country_name":"Belgium"
   },
   {  
      "country_id":"22",
      "country_name":"Belize"
   },
   {  
      "country_id":"23",
      "country_name":"Benin"
   },
   {  
      "country_id":"24",
      "country_name":"Bermuda"
   }
]

仅举几个国家。

我可以成功解析数据,并通过@jaime建议的angular$htttp.get()服务从控制器将其填充到UI中:

     //Getting the base url inorder to tell app where to find countries json
     var baseUrl = $location.absUrl().substring(0, $location.absUrl().indexOf('www')); 

                    //Fetching the countries Json
                    return $http.get(baseUrl+'www/json/countries.json')

                    //On Fetching the countries lsit
                    .then( function(response){
                        $scope.countryList =  response.data;
                    });

毫无疑问,它运作良好。 但是还有另一种方法可以实现此功能吗? 不使用$ http.get()? 我遇到了angular.fromJson() ,但是它不会解析文件路径,因为它需要将参数作为json对象。 我也遇到了使用jquery的替代方法,如https://stackoverflow.com/a/12091134/1904479 还有其他不需要jQuery的替代方法,而是使用角度或离子代码吗?

实际上,我们可以通过在常量文件中将其声明为:

步骤1)创建一个文件constants.js并将json对象添加到其中:

var ConstantsCountries  =  [  
       {  
          "country_id":"1",
          "country_name":"Afghanistan"
       },
       {  
          "country_id":"2",
          "country_name":"Albania"
       },
       {  
          "country_id":"3",
          "country_name":"Algeria"
       },
       {  
          "country_id":"4",
          "country_name":"American Samoa"
       },
       {  
          "country_id":"5",
          "country_name":"Andorra"
       },
       {  
          "country_id":"6",
          "country_name":"Angola"
       },
       {  
          "country_id":"7",
          "country_name":"Anguilla"
       },
       {  
          "country_id":"8",
          "country_name":"Antarctica"
       },
       {  
          "country_id":"9",
          "country_name":"Antigua and Barbuda"
       },
       {  
          "country_id":"10",
          "country_name":"Argentina"
       },
       {  
          "country_id":"11",
          "country_name":"Armenia"
       },
       {  
          "country_id":"12",
          "country_name":"Aruba"
       },
       {  
          "country_id":"13",
          "country_name":"Australia"
       },
       {  
          "country_id":"14",
          "country_name":"Austria"
       },
       {  
          "country_id":"15",
          "country_name":"Azerbaijan"
       },
       {  
          "country_id":"16",
          "country_name":"Bahamas"
       },
       {  
          "country_id":"17",
          "country_name":"Bahrain"
       },
       {  
          "country_id":"18",
          "country_name":"Bangladesh"
       },
       {  
          "country_id":"19",
          "country_name":"Barbados"
       }
    ]

第2步:将js文件包含在index.html如下所示:

<!-----Constants Classes---->
    <script src="Constants/Constants.js"></script>

步骤3:在您的控制器中以下列方式使用它:

.controller('CountriesCtrl', function ($localStorage,$rootScope,$location,$ionicLoading,$ionicHistory,$timeout) { 
    $scope.countries = ConstantsCountries;
    console.log(angular.toJson($scope.countries));
}))

暂无
暂无

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

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