zoukankan      html  css  js  c++  java
  • AngularJs的$http使用随笔

    AngularJs的$http服务是Angularjs自带的核心服务之一,用来与HTTP远程服务器交互。

    关于$http使用,我体会的一下几点注意:

    1.在使用是报“Uncaught ReferenceError: $http is not defined”这个错

       是因为没有在方法中引入$http -> function($http){}

    1 function($http){
    2   $http({
    3     method:'GET',
    4     url:'a.json'
    5   }).success(function(data){
    6     $scope.model = data[0];
    7   })
    8 }

     2.怎么读取json文件内容

     1 [
     2    {
     3         "name":"xiaoli",
     4         "age":"18",
     5         "sex":"boy"
     6    },
     7    {
     8         "name":"xiaohong",
     9         "age":"19",
    10         "sex":"girl"
    11    }   
    12 ]

    那么访问代码如下:

     1 function($http){
     2   $http({
     3     //发送GET请求
     4     method:'GET',
     5     //访问a.json文件
     6     url:'a.json'
     7   }).success(function(data){
     8     //访问成功,返回data数据
     9     //将data数据的第一条赋值给$scope.model
    10     $scope.model = data[0];
    11   })
    12 }

    在上面代码中,我的json文件是一个数组,所以访问完了返回回来的data数据也是一个json数组,输出对应数组即可。特定的字段为:data[0].name;

  • 相关阅读:
    Java Web开发 之VO、PO、DTO等收集
    Hive的安装与使用
    各种默认回车提交表单
    A/B Problem
    A+B Problem II
    A+B Problem IV
    关于521
    劝你别做
    无主之地1
    A+B Problem(V)
  • 原文地址:https://www.cnblogs.com/guyunxiang/p/3673729.html
Copyright © 2011-2022 走看看