AngularJS 入门2-服务与$http
来源:互联网 发布:淘宝天天特价在哪里找 编辑:程序博客网 时间:2024/06/05 07:10
AngularJS 入门2-服务与$http
1.AngularJS 服务(Service)
AngularJS 中你可以创建自己的服务,或使用内建服务。
AngularJS 中,服务是一个函数或对象,可在你的 AngularJS应用中使用。
AngularJS 内建了30多个服务。
常用内建服务:
① $location 服务,它可以返回当前页面的 URL地址。
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $location) {
$scope.myUrl = $location.absUrl();
});
②$http 是AngularJS 应用中最常用的服务。发送请求,接受数据。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("welcome.htm").then(function (response) {
$scope.myWelcome = response.data;
});
});
③$timeout 服务,对应了 JS window.setTimeout 函数。
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 服务,对应了 JS window.setInterval 函数。
每一秒显示信息:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
$scope.theTime = new Date().toLocaleTimeString();
$interval( function () {
$scope.theTime = new Date().toLocaleTimeString();
} , 1000);
});
创建自定义服务,服务就是一个函数
创建名为hexafy 的服务:
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16);
}
});
使用自定义的的服务 hexafy 将一个数字转换为16进制数:
app.controller('myCtrl', function($scope, hexafy) {
$scope.hex = hexafy.myFunc(255);
});
2.AngularJS 的$http服务
$http 是 AngularJS中的一个核心服务,用于发送请求并读取远程服务器的数据。
以下是存储在web服务器上的JSON 文件:
http://www.runoob.com/try/angularjs/data/sites.php
{
"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"
}
]
}
废弃声明 (v1.5)
v1.5 中$http 的 success 和 error 方法已废弃。使用 then 方法替代。
AngularJS1.5 以上版本 - 通用方法 -实例
var app = angular.module('myApp', []);
app.controller('siteCtrl', function($scope, $http) {
$http({
method: 'GET',
url: 'https://www.runoob.com/try/angularjs/data/sites.php'
}).then(function successCallback(response) {
$scope.names = response.data.sites;
}, function errorCallback(response) {
// 请求失败执行代码
});
});
AngularJS1.5 以上版本 - 简写方法- 实例
var app = angular.module('myApp', []);
app.controller('siteCtrl', function($scope, $http) {
$http.get("http://www.runoob.com/try/angularjs/data/sites.php")
.then(function (response) {$scope.names = response.data.sites;});
});
AngularJS1.5 以下版本- 简写方法 - 实例
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;});
});
- AngularJS 入门2-服务与$http
- AngularJs $http 请求服务
- angularjs之http服务
- AngularJs服务-http请求
- AngularJS $http服务例子
- angularjs-http服务
- AngularJS中的 $http服务
- AngularJs内置服务$http
- AngularJS 内置服务:$http
- angularjs的 $http 服务
- AngularJs $http 请求服务
- AngularJs $http 请求服务
- AngularJS中$http服务
- angularJs中的http服务
- AngularJS http 服务
- 使用AngularJS的$http服务与服务端进行数据交互
- AngularJs $http 请求服务整理
- AngularJS服务$http,$interval,$timeout
- strut2
- 还是安利几本书,关于操作系统和计算机组成原理的
- java 4种方式读取配置文件 + 修改配置文件
- Java内部类的详解
- 设计模式之外观
- AngularJS 入门2-服务与$http
- Poedu_Windows_IOCP初窥
- 事务模型与分布式事务总结思考
- Activity启动流程
- Akka(8): 分布式运算:Remoting-远程查找式
- linux上tomcat启动没问题,访问不了项目
- Java中String、StringBuilder和StringBuffer的区别
- 给定字符串 str,检查其是否包含数字,包含返回 true,否则返回 false
- AngularJS 入门3-选择框和表格