习笔记-AngularJs(四)

事先学习之事视图与模版,我们当控制器文件中一直定义一个屡次组,让那个于模板文件中因故ng-repeat指令构造一个迭代器,定义之数组http://t.cn/RUbL4rP犹以下:

    $scope.phones = [
        {'name':'xioabin','number':'18824863682','age':'12'},
        {'name':'xioalong','number':'18824863683','age':'19'},
        {'name':'xiaohua','number':'18824863684','age':'5'},
        {'name':'xiaoMING','number':'18824863685','age':'1'},
        {'name':'xiaoTU','number':'18824863686','age':'2'},
        {'name':'xiaoKE','number':'18824863687','age':'40'},
    ];

这种样式反复无是咱所假设的,我们一般会以常用ajax技术去获取数据,在angular也产生接近之劳务来落实XHR,那就是
$http,使用怎么一个劳务,需要拿代码放置于地方服务器或是web站点上,首先先准备phone.json文件,如下:

[

        {"name":"xioabin","number":"18824863682","age":"12"},

        {"name":"xioalong","number":"18824863683","age":"19"},

        {"name":"xiaohua","number":"18824863684","age":"5"},

        {"name":"xiaoMING","number":"18824863685","age":"1"},

        {"name":"xiaoTU","number":"18824863686","age":"2"},

        {"name":"xiaodfs","number":"18824863687","age":"46"},

        {"name":"xiaodfE","number":"18824863682","age":"46"},

        {"name":"xiaobh","number":"18824863680","age":"48"},

        {"name":"xiaogg","number":"18824863687","age":"10"},

        {"name":"xiaouu","number":"18824863686","age":"20"},

        {"name":"xiaoKds","number":"18824863682","age":"30"},

        {"name":"xiaoKEdad","number":"18824863689","age":"60"},

        {"name":"xiaoKb","number":"18824863683","age":"90"},

        {"name":"xiaofa","number":"18824863685","age":"17"}

    ]

内容可以协调设置,模版文件代码和之前大同小异:

<!doctype html>

<html ng-app ng-controller="PhoneListCtrl">

  <head>

      <meta charset='utf8' />

      <title ng-bind="'Google Phone Gallery:' + query"></title>  

      <!-- <title ng-bind-template="Google Phone Gallery:{{query}}"></title> -->

    <script src="http://code.angularjs.org/angular-1.0.1.min.js"></script>

    <script src="script.js"></script>

  </head>

  <body>

    <div class="example2">

        Select:  

        <select name="select" id="select" ng-model='order'>

            <option value="name">名字</option>

            <option value="age">年龄</option>

        </select>                        

        Search: <input name="search" type="text" ng-model='query' />

         <ul>

         <!--迭代器-->
    <li>
      序号&nbsp;&nbsp; 
      姓名&nbsp;&nbsp; 
      号码&nbsp;&nbsp; 
      年龄
    </li>
         <li ng-repeat='phone in phones | filter:query | orderBy:order'>

               {{$index+1}}&nbsp;&nbsp;  {{phone.name}}&nbsp; &nbsp; {{phone.number}}&nbsp; &nbsp; {{phone.age}}

         </li>

         </ul>

    </div>

  </body>

</html>

不等之便是控制器文件的差,如下:

//注入服务$http
function PhoneListCtrl($scope,$http){
      $http.get("phone.json").success(function(data, status, headers, config) {
          if(status==200){ $scope.phones = data;  }
        console.log(status+","+headers+","+config);
        // alert(JSON.stringify(data));
      });
        $scope.order = 'name';
}

这边传出了一个$http,那么我们就是得透过$http.get(路径).success(function(data,status){/*马到成功获取数据,之后该干嘛?*/}),data是返回的数,status是状态码,header和config可以打印出来看一下,应该是局部部署和头颅吧!这样$scope.phones就与前面同一是一个数组了!

官网的$http是这么的花样,可以参见着写:

$http({  
        url:'...',
           method:'...',
           data:'...',
          params:'...',
          cache:'...'
      })
         .success(function(){....})
         .error(function() {.....});

假使我们的控制器按点那样写的语句,在减代码时候会拧,那么闹这点儿种办法好化解这问题:

为克服压缩引起的问题,只要以控制器函数里面为$inject属性赋值一个靠服务标识符的数组,就比如于诠释掉那段最后一实施那样:

PhoneListCtrl.$inject = [‘$scope’, ‘$http’];

其它一样种办法为足以为此来指定依赖列表并且避免压缩问题——使用Javascript数组方式组织控制器:把要注入的劳动放到一个字符串数组(代表乘之讳)里,数组最后一个要素是控制器的法子函数:

var PhoneListCtrl = [‘$scope’, ‘$http’, function($scope, $http) { /*
constructor body */ }];

这就是angularjs的据注入了!当控制器构造的时候,AngularJS的靠注入器会将这些服务注入及公的控制器中。当然,依赖注入器也会处理所要服务或者存在的外传递性依赖(一个劳动普通会凭借让外的劳务)。

顾不要动‘$’前缀来定名你自己之服务以及模型(就是和睦得定义自己之劳动,像$http),否则恐怕会见生名字冲突。

 

相关文章