angular常用的内建服务
来源:互联网 发布:js失去焦点事件 编辑:程序博客网 时间:2024/06/07 19:30
在 AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用。
AngularJS 内建了30 多个服务。
1.$http
$http 是 AngularJS 应用中最常用的服务。 服务向服务器发送请求,应用响应服务器传送过来的数据。例:
使用格式:
//简单的GET请求,可以改为POST
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
//请求成功执行代码
}, function errorCallback(response) {
//请求失败执行代码
});
简写方法:POST与GET简写方法格式:
$http.get('/someUrl', config).then(successCallback, errorCallback);;
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
此外还有以下简写方法:
$http.get
$http.head
$http.post
$http.put
$http.delete
$http.jsonp
$http.patch
通用方法实例:
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
$http({
method: 'GET',
url: 'https://www.runoob.com/try/angularjs/data/sties.php'
}).then(function successCallback (response) {
$scope.list = response.data.sites;
}, function errorCallback (response) {
//请求失败执行代码
});
});
简写方法实例:
<div ng-app='myApp' ng-controller='siteCtrl'>
<ul>
<li ng-repeat="li in list">
{{ li.name + ',' + li.country }}
</li>
</ul>
</div>
var app = angular.module('myApp', []);
app.controller('siteCtrl', function ($scope, $http) {
$http.get('http://www.../../someUrl.php').then(function (response) {
$scope.list = response.data.sites;
});
});
2.$timeout
$timeout(function () {
$scope.txt = "hello kitty!";
}, 2000);
3.$interval
js里的setInterval()方法:
setInterval()方法可按照指定的周期(毫秒计)调用函数或表达式;
setInterval()方法会不停的调用函数,直到clearInterval()关闭;setInterval()返回的ID值可用作clearInterval()方法的参数;
例:
<input type="text" id="clock" size="50">
<button onclick="int=window.clearInterval(int)"> stop this count </button>
<script>
var int = self.setInterval("count()", 500);
function count () {
var t = new Date();
document.getElementById("clock").value = t;
}
</script>
例:angular时钟:
<p>{{ theTime }}</p>
$scope.theTime = new Date().toLocaleTimeString();
$interval(function () {
$scope.theTime = new Date().loLocaleTimeString();
}, 1000);
4.$location
(1)获取当前完整的url路径:$location.absUrl();
(2)获取当前url路径(#后面的内容):$location.url();
(3)修改url的子路径(#后面内容):$location.url('/nameMaket/schdule_cbs' + proNo);
5.$watch
$watch(watchExpression,listener,objectEquality);
watchExpression: 监听的对象,它可以是angular的一个表达式如'name',或函数如function () { return $scope.name }。
listener: 当watchExpression变化时,会被调用的函数。它有3个参数:newValue(新值),oldValue(旧值),scope(作用域的引用)。
objectEquality: 是否深度监听。若为true,它告诉angular检查所监控的对象中每一个属性的变化。若你希望监控数组的个别元素、或对象的属性等,应用'true'。
例:
$scope.name = "hello";
var watch = $scope.$watch('name', function (newValue, oldValue, scope) {
console.log(newValue);
console.log(oldValue);
});
$timeout(function () {
$scope.name = "hello nmb world!";
}, 1000);
注:$watch将导致性能问题,$watch如果不再使用,最好将其注销。
最后,$watch会返回一个函数,这个函数可以用来注销监听,只需要被调用一次即可:
var destroy = $scope.$watch(...);
destroy();
- angular常用的内建服务
- angular的http服务的常用方式
- angular的常用的三种服务 service,factory,provider
- angular的$interpolate服务
- angular的provider服务
- angular的$http服务
- angular $location服务的配置
- angular的服务小总结
- angular 简单的服务应用
- angular的service服务和factory服务
- Angular的服务之自定义服务
- Angular 比较常用的指令
- Angular 比较常用的指令
- angular服务
- angular-服务
- Angular服务
- angular 服务
- angular服务
- Spring 自动装载
- Python中 sys.argv[]的用法简明解释
- 【pandas】[1] Series 入门(创建和增删改查)
- RedHat linux 下kdump的配置
- HttpClients多文件上传连接 WebODM 中创建任务接口的方法
- angular常用的内建服务
- 【C++笔记】表达式 & 语句
- 数据结构上机实践第九周项目3
- PHP设计模式之外观(门面)模式
- 我的第一篇博客
- Linux系统关机重启命令总结
- Socket中的read方法阻塞问题
- 原码、反码、补码的正(nao)确(can)打开方式
- DOM复制节点