国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngularJS入門教程之 XMLHttpRequest實(shí)例講解

2019-11-20 09:23:48
字體:
供稿:網(wǎng)友

AngularJS XMLHttpRequest

$http 是 AngularJS 中的一個(gè)核心服務(wù),用于讀取遠(yuǎn)程服務(wù)器的數(shù)據(jù)。

讀取 JSON 文件

以下是存儲(chǔ)在web服務(wù)器上的 JSON 文件:

http://www.runoob.com/try/angularjs/data/Customers_JSON.php

{"records":[{"Name" : "Alfreds Futterkiste","City" : "Berlin","Country" : "Germany"},{"Name" : "Berglunds snabbköp","City" : "Luleå","Country" : "Sweden"},{"Name" : "Centro comercial Moctezuma","City" : "México D.F.","Country" : "Mexico"},{"Name" : "Ernst Handel","City" : "Graz","Country" : "Austria"},{"Name" : "FISSA Fabrica Inter. Salchichas S.A.","City" : "Madrid","Country" : "Spain"},{"Name" : "Galería del gastrónomo","City" : "Barcelona","Country" : "Spain"},{"Name" : "Island Trading","City" : "Cowes","Country" : "UK"},{"Name" : "Königlich Essen","City" : "Brandenburg","Country" : "Germany"},{"Name" : "Laughing Bacchus Wine Cellars","City" : "Vancouver","Country" : "Canada"},{"Name" : "Magazzini Alimentari Riuniti","City" : "Bergamo","Country" : "Italy"},{"Name" : "North/South","City" : "London","Country" : "UK"},{"Name" : "Paris spécialités","City" : "Paris","Country" : "France"},{"Name" : "Rattlesnake Canyon Grocery","City" : "Albuquerque","Country" : "USA"},{"Name" : "Simons bistro","City" : "København","Country" : "Denmark"},{"Name" : "The Big Cheese","City" : "Portland","Country" : "USA"},{"Name" : "Vaffeljernet","City" : "Århus","Country" : "Denmark"},{"Name" : "Wolski Zajazd","City" : "Warszawa","Country" : "Poland"}]}

AngularJS $http

AngularJS $http 是一個(gè)用于讀取web服務(wù)器上數(shù)據(jù)的服務(wù)。

$http.get(url) 是用于讀取服務(wù)器數(shù)據(jù)的函數(shù)。

AngularJS 實(shí)例

<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="customersCtrl"> <ul> <li ng-repeat="x in names">  {{ x.Name + ', ' + x.Country }} </li></ul></div><script>var app = angular.module('myApp', []);app.controller('customersCtrl', function($scope, $http) { $http.get("http://www.runoob.com/try/angularjs/data/Customers_JSON.php") .success(function (response) {$scope.names = response.records;});});</script></body></html>

運(yùn)行結(jié)果:

  • Alfreds Futterkiste, Germany
  • Ana Trujillo Emparedados y helados, Mexico
  • Antonio Moreno Taquería, Mexico
  • Around the Horn, UK
  • B's Beverages, UK
  • Berglunds snabbköp, Sweden
  • Blauer See Delikatessen, Germany
  • Blondel père et fils, France
  • Bólido Comidas preparadas, Spain
  • Bon app', France
  • Bottom-Dollar Marketse, Canada
  • Cactus Comidas para llevar, Argentina
  • Centro comercial Moctezuma, Mexico
  • Chop-suey Chinese, Switzerland
  • Comércio Mineiro, Brazil

應(yīng)用解析:

注意:以上代碼的 get 請(qǐng)求是本站的服務(wù)器,你不能直接拷貝到你本地運(yùn)行,會(huì)存在跨域問題,解決辦法就是將

Customers_JSON.php 的數(shù)據(jù)拷貝到你自己的服務(wù)器上,附:PHP Ajax 跨域問題最佳解決方案。

AngularJS 應(yīng)用通過 ng-app 定義。應(yīng)用在 <div> 中執(zhí)行。

ng-controller 指令設(shè)置了 controller 對(duì)象 名。

函數(shù) customersController 是一個(gè)標(biāo)準(zhǔn)的 JavaScript 對(duì)象構(gòu)造器。

控制器對(duì)象有一個(gè)屬性: $scope.names。

$http.get() 從web服務(wù)器上讀取靜態(tài) JSON 數(shù)據(jù)。

服務(wù)器數(shù)據(jù)文件為:  http://www.runoob.com/try/angularjs/data/Customers_JSON.php。

當(dāng)從服務(wù)端載入 JSON 數(shù)據(jù)時(shí),$scope.names 變?yōu)橐粋€(gè)數(shù)組。

注意:以上代碼也可以用于讀取數(shù)據(jù)庫數(shù)據(jù)。

以上就是對(duì)AngularJS XMLHttpRequest資料的整理,后續(xù)繼續(xù)補(bǔ)充,希望能幫助有需要的朋友。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 翼城县| 武乡县| 肥东县| 广丰县| 措勤县| 聊城市| 永安市| 贵定县| 涟水县| 桂平市| 安平县| 奉化市| 扎兰屯市| 绍兴市| 兴海县| 隆安县| 广饶县| 横峰县| 满城县| 枣庄市| 沙河市| 广丰县| 迁安市| 清镇市| 滨州市| 吉水县| 噶尔县| 南华县| 双鸭山市| 东丽区| 西林县| 五台县| 建始县| 柞水县| 兴业县| 云梦县| 寻乌县| 色达县| 墨江| 齐河县| 伊金霍洛旗|