美文网首页
AngularJS Service+Http+Select+表格

AngularJS Service+Http+Select+表格

作者: 鹿守心畔光 | 来源:发表于2016-12-14 18:38 被阅读165次

AngularJS 服务(Service)

AngularJS 中你可以创建自己的服务,或使用内建服务。


什么是服务?

<small>在 AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用。
AngularJS 内建了30 多个服务。
有个 $location 服务,它可以返回当前页面的 URL 地址。
实例

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $location) {    
   $scope.myUrl = $location.absUrl();
});

尝试一下 »
注意 $location 服务是作为一个参数传递到 controller 中。如果要使用它,需要在 controller 中定义。</small>


为什么使用服务?

<small>在很多服务中,比如 $location 服务,它可以使用 DOM 中存在的对象,类似 window.location 对象,但 window.location 对象在 AngularJS 应用中有一定的局限性。
AngularJS 会一直监控应用,处理事件变化, AngularJS 使用 $location 服务比使用 window.location 对象更好。
$location vs window.location

目的 允许对当前浏览器位置进行读写操作 允许对当前浏览器位置进行读写操作
API 暴露一个"裸聊"的能被读写的对象 暴露jquery风格的读写器
是否在AngularJS应用生命周期中和应用整合 可获取到应用声明周期内的每一个阶段,并且和$watch整合
是否和HTML5 API的无缝整合 是(对低级浏览器优雅降级)
和应用的上下文是否相关 否,window.location.path 返回"/docroot/actual/path" 是,$location.path()返回"/actual/path"

</small>


$http 服务

<small>$http 是 AngularJS 应用中最常用的服务。 服务向服务器发送请求,应用响应服务器传送过来的数据。</small>
实例
<small>使用 $http 服务向服务器请求数据:</small>

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {    
    $http.get("welcome.htm").then(function (response) {        
        $scope.myWelcome = response.data;    
    });
});

尝试一下 »
<small>以上是一个非常简单的 $http 服务实例,更多 $http 服务应用请查看 AngularJS Http 教程。</small>


$timeout 服务

<small>AngularJS $timeout 服务对应了 JS window.setTimeout 函数。</small>
实例
<small>两秒后显示信息:</small>

var app = angular.module('myApp', []);app.controller('myCtrl', function($scope, $timeout) {    
   $scope.myHeader = "Hello World!";    
   $timeout(function () {        
      $scope.myHeader = "How are you today?";    
   }, 2000);
});

尝试一下 »


$interval 服务

<small>AngularJS $interval 服务对应了 JS window.setInterval 函数。</small>
实例
<small>每两秒显示信息:</small>

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {    
   $scope.theTime = new Date().toLocaleTimeString();    
   $interval(function () {        
       $scope.theTime = new Date().toLocaleTimeString();    
   }, 1000);
});

尝试一下 »


创建自定义服务

<small>你可以创建访问自定义服务,链接到你的模块中:
创建名为hexafy 的访问:</small>

app.service('hexafy', function() {    
    this.myFunc = function (x) {        
        return x.toString(16);    
    }
});

<small>要使用访问自定义服务,需要在定义过滤器的时候独立添加:
实例
使用自定义的的服务 hexafy 将一个数字转换为16进制数:</small>

app.controller('myCtrl', function($scope, hexafy) {    
    $scope.hex = hexafy.myFunc(255);
});

尝试一下 »


过滤器中,使用自定义服务

<small>当你创建了自定义服务,并连接到你的应用上后,你可以在控制器,指令,过滤器或其他服务中使用它。
在过滤器 myFormat 中使用服务 hexafy:</small>

app.filter('myFormat',['hexafy', function(hexafy) {    
    return function(x) {        
        return **hexafy**.myFunc(x);    
    };
}]);

尝试一下 »
<small>在对象数组中获取值时你可以使用过滤器:
创建服务 hexafy:</small>

<ul>
  <li ng-repeat="x in counts">{{x | myFormat}}</li>
</ul>

尝试一下 »



AngularJS XMLHttpRequest

$http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据。


读取 JSON 文件

<small>以下是存储在web服务器上的 JSON 文件:</small>
<code>http://www.runoob.com/try/angularjs/data/sites.php</code>

{ 
    "sites": [ 
        { 
            "Name": "菜鸟教程", 
            "Url": "www.runoob.com", 
            "Country": "CN" 
        }, 
        { 
            "Name": "Google", 
            "Url": "www.google.com", 
            "Country": "USA" 
        }, 
        { 
            "Name": "Facebook", 
            "Url": "www.facebook.com", 
            "Country": "USA" 
        }, 
        { 
            "Name": "微博", 
            "Url": "www.weibo.com", 
            "Country": "CN" 
        } 
    ]
}

AngularJS $http

<small>AngularJS $http 是一个用于读取web服务器上数据的服务。
$http.get(url) 是用于读取服务器数据的函数。</small>
AngularJS 实例

<div ng-app="myApp" ng-controller="siteCtrl"> 
  <ul> 
    <li ng-repeat="x in names"> 
      {{ x.Name + ', ' + x.Country }} 
    </li>
  </ul> 
</div> 
<script>
  var app = angular.module('myApp', []);
  app.controller('siteCtrl', function($scope, $http) { 
    $http.get("http://www.runoob.com/try/angularjs/data/sites.php") 
    .success(function (response) {$scope.names = response.sites;})
  ;});
</script>

尝试一下 »
<small>应用解析:
�注意:以上代码的 get 请求是本站的服务器,你不能直接拷贝到你本地运行,会存在跨域问题,解决办法就是将 Customers_JSON.php 的数据拷贝到你自己的服务器上,附:PHP Ajax 跨域问题最佳解决方案
AngularJS 应用通过 ng-app 定义。应用在 <div> 中执行。
ng-controller 指令设置了 controller 对象 名。
函数 customersController 是一个标准的 JavaScript 对象构造器
控制器对象有一个属性: $scope.names
$http.get() 从web服务器上读取静态 JSON 数据
服务器数据文件为: http://www.runoob.com/try/angularjs/data/sites.php
当从服务端载入 JSON 数据时,$scope.names 变为一个数组。</small>



AngularJS Select(选择框)

AngularJS 可以使用数组或对象创建一个下拉列表选项。


使用 ng-options 创建选择框

<small>在 AngularJS 中我们可以使用 ng-option 指令来创建一个下拉列表,列表项通过对象和数组循环输出,如下实例:
实例</small>

<div ng-app="myApp" ng-controller="myCtrl">
  <select ng-model="selectedName" ng-options="x for x in names">
  </select>
</div>

<script>
  var app = angular.module('myApp', []);
  app.controller('myCtrl', function($scope) {    
    $scope.names = ["Google", "Runoob", "Taobao"];
  });
</script>

尝试一下 »


ng-options 与 ng-repeat

<small>我们也可以使用ng-repeat 指令来创建下拉列表:
实例</small>

<select>
  <option ng-repeat="x in names">{{x}}</option>
</select>

尝试一下 »
<small>ng-repeat 指令是通过数组来循环 HTML 代码来创建下拉列表,但 ng-options 指令更适合创建下拉列表,它有以下优势:
使用 ng-options 的选项的一个对象, ng-repeat 是一个字符串。</small>


应该用哪个更好?

<small>假设我们使用以下对象:</small>

$scope.sites = [ 
    {site : "Google", url : "http://www.google.com"}, 
    {site : "Runoob", url : "http://www.runoob.com"}, 
    {site : "Taobao", url : "http://www.taobao.com"}
];

<small>ng-repeat 有局限性,选择的值是一个字符串:</small>
实例
<small>使用 ng-repeat:</small>

<select ng-model="selectedSite">
  <option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option>
</select>

<h1>你选择的是: {{selectedSite}}</h1>

尝试一下 »
<small>使用 ng-options 指令,选择的值是一个对象:</small>
实例
<small>使用 ng-options:</small>

<select ng-model="selectedSite" ng-options="x.site for x in sites">
</select>

<h1>你选择的是: {{selectedSite.site}}</h1>
<p>网址为: {{selectedSite.url}}</p>

尝试一下 »
<small>当选择值是一个对象时,我们就可以获取更多信息,应用也更灵活。</small>


数据源为对象

<small>前面实例我们使用了数组作为数据源,以下我们将数据对象作为数据源。</small>

$scope.sites = { 
    site01 : "Google", 
    site02 : "Runoob", 
    site03 : "Taobao"
};

<small>ng-options 使用对象有很大的不同,如下所示:</small>
实例
<small>使用对象作为数据源, x 为键(key), y 为值(value):</small>

<select ng-model="selectedSite" ng-options="**x for (x, y) in sites**">
</select>

<h1>你选择的值是: {{selectedSite}}</h1>

尝试一下 »
<small>你选择的值为在 key-value 对中的 value
value 在 key-value 对中也可以是个对象:</small>
实例
<small>选择的值在 key-value 对的 value 中, 这是它是一个对象:</small>

$scope.cars = {
  car01 : {brand : "Ford", model : "Mustang", color : "red"},
  car02 : {brand : "Fiat", model : "500", color : "white"},
  car03 : {brand : "Volvo", model : "XC90", color : "black"
}};

尝试一下 »
<small>在下拉菜单也可以不使用 key-value 对中的 key , 直接使用对象的属性:</small>
实例

<select ng-model="selectedCar" ng-options="**y.brand** for (x, y) in cars">
</select>

尝试一下 »



AngularJS 表格

ng-repeat 指令可以完美的显示表格。


在表格中显示数据

<small>使用 angular 显示表格是非常简单的:</small>
AngularJS 实例

<div ng-app="myApp" ng-controller="customersCtrl"> 
  <table>  
    <tr ng-repeat="x in names">    
      <td>{{ x.Name }}</td>    
      <td>{{ x.Country }}</td>  
    </tr>
  </table>
</div>
<script>
  var app = angular.module('myApp', []);
  app.controller('customersCtrl', function($scope, $http) {    
    $http.get("/try/angularjs/data/Customers_JSON.php")    
    .success(function (response) {$scope.names = response.records;});});
</script>

尝试一下 »


使用 CSS 样式

<small>为了让页面更加美观,我们可以在页面中使用CSS:</small>
CSS 样式

<style>
  table, th , td {  
    border: 1px solid grey;  
    border-collapse: collapse;  
    padding: 5px;
  }
  table tr:nth-child(odd) {  
    background-color: #f1f1f1;
  }
  table tr:nth-child(even) {  
    background-color: #ffffff;
  }
</style>

尝试一下 »


使用 orderBy 过滤器

<small>排序显示,可以使用 orderBy 过滤器: </small>
AngularJS 实例

<table>  
  <tr ng-repeat="x in names | orderBy : 'Country'">    
    <td>{{ x.Name }}</td>    
    <td>{{ x.Country }}</td>  
  </tr>
</table>

尝试一下 »


使用 uppercase 过滤器

<small>使用 uppercase 过滤器转换为大写: </small>
AngularJS 实例

<table>  
  <tr ng-repeat="x in names">    
    <td>{{ x.Name }}</td>    
    <td>{{ x.Country | uppercase }}</td>  
  </tr>
</table>

尝试一下 »


显示序号 ($index)

<small>表格显示序号可以在 <td> 中添加 $index: </small>
AngularJS 实例

<table>  
  <tr ng-repeat="x in names">    
    <td>{{ $index + 1 }}</td>    
    <td>{{ x.Name }}</td>    
    <td>{{ x.Country }}</td>  
  </tr>
</table>

尝试一下 »


使用 $even 和 $odd

AngularJS 实例

<table>
  <tr ng-repeat="x in names">
    <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Name }}</td>
    <td ng-if="$even">{{ x.Name }}</td>
    <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Country }}</td>
    <td ng-if="$even">{{ x.Country }}</td>
  </tr>
</table>

尝试一下 »

相关文章

网友评论

      本文标题:AngularJS Service+Http+Select+表格

      本文链接:https://www.haomeiwen.com/subject/iqaimttx.html