??????? $http是AngularJS 中的一個(gè)核心服務(wù),用于讀取遠(yuǎn)程服務(wù)器的數(shù)據(jù)。
??????? 以下是存儲在web服務(wù)器上的 JSON 文件data.json。
[ { "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是一個(gè)用于讀取web服務(wù)器上數(shù)據(jù)的服務(wù)。$http.get(url) 是用于讀取服務(wù)器數(shù)據(jù)的函數(shù)。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8"></meta> <title>AngularJS XMLHttpRequest</title> <script src="angular/angular.js"></script> </head> <body> <div ng-app="" ng-controller="customersController"> <ul> <li ng-repeat="x in names">{{ x.Name + ', ' + x.Country }}</li> </ul> </div> </body> <script> function customersController($scope,$http) { $http.get("data.json").success(function(response) {$scope.names = response;}); //$http.get("http://localhost:8088/angularjs-http/data.json").success(function(response) {$scope.names = response;}); } </script> </html>
應(yīng)用解析
:
1.AngularJS 應(yīng)用通過 ng-app 定義。應(yīng)用在 <div> 中執(zhí)行。
2.ng-controller 指令設(shè)置了controller 對象名。
3.函數(shù) customersController 是一個(gè)標(biāo)準(zhǔn)的 JavaScript 對象構(gòu)造器。
4.控制器對象有一個(gè)屬性: $scope.names。
5.$http.get() 從web服務(wù)器上讀取靜態(tài) JSON 數(shù)據(jù)。
6.當(dāng)從服務(wù)端載入 JSON 數(shù)據(jù)時(shí),$scope.names 變?yōu)橐粋€(gè)數(shù)組。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
