In Angular there are several built in services. $http service is one of them. In this video, we will discuss another built in service, $log. It is also possible to create our own custom services in Angular. 

At this point several questions come to our mind

  • What are services in Angular
  • When should we be creating services in Angular
  • How to create our own custom Angular services
  • Where do they fit, in an angular application architecture
  • What are the benefits of using services

I will answer all these questions in a later video. The reason for postponing this discussion, is that, it is easier to understand the concept of Angular services and the benefits they provide, once we understand how to use use 1 or 2 built in angular services. 

So, let's start our discussion with $http service. 

$http service in Angular is used to make HTTP requests to remote server 

$http service is a function that has a single input parameter i.e a configuration object. 

Example : The following example issues a GET request to the specified URL

$http({
    method: 'GET',
    url: 'EmployeeService.asmx/GetAllEmployees'
});

In the example above we are only using 2 properties of the configuration object. Check the link below for the complete list of properties supported by the configuration object
https://docs.angularjs.org/api/ng/service/$http#usage

Shortcut methods like get, post, put, delete etc are also available to be used with $http service

Example : Using the short cut method get()
$http.get('EmployeeService.asmx/GetAllEmployees')

$http service returns a promise object. This means the functions are executed asynchronously and the data that these functions return may not be available immediately. Because of this reason you cannot use the return value of the $http service as shown below.

$scope.employees = $http.get('EmployeeService.asmx/GetAllEmployees');

Instead you will use the then() method. The successCallback function that is passed as the parameter to the then function is called when the request completes. The successCallback function receives a single object that contains several properties. Use the data property of the object to retrieve the data received from the server.

$scope.employees = $http.get('EmployeeService.asmx/GetAllEmployees')
            .then(function (response) {
                $scope.employees = response.data;
            });

You can use the $log service to log the response object to the console to inspect all of it's properties

$scope.employees = $http.get('EmployeeService.asmx/GetAllEmployees')
                        .then(function (response) {
                            $scope.employees = response.data;
                            $log.info(response);
                        });

If there is an error processing the request, the errorCallback function is called. The errorCallback function is passed as the second parameter to the then() function. The errorCallback function receives a single object that contains several properties. Use the data or statusText properties of the returned object to find the reasons for the failure.

$scope.employees = $http.get('EmployeeService.asmx/GetAllEmployee')
                        .then(function (response) {
                            $scope.employees = response.data;
                        }, function (reason) {
                            $scope.error = reason.data;
                        });

You can use the $log service to log the response object to the console to inspect all of it's properties

$scope.employees = $http.get('EmployeeService.asmx/GetAllEmployee')
                        .then(function (response) {
                            $scope.employees = response.data;
                        }, function (reason) {
                            $scope.error = reason.data;
                            $log.info(reason);
                        });

You can also create separate functions and associate them as successCallback and errorCallback functions

var successCallBack = function (response) {
    $scope.employees = response.data;
};
 
var errorCallBack = function (reason) {
    $scope.error = reason.data;
}
 
$scope.employees = $http.get('EmployeeService.asmx/GetAllEmployees')
                        .then(successCallBack, errorCallBack);

Default Transformations provided by Angular's http service

    • If the data property of the request configuration object contains a JavaScript object, it is automatically converted into JSON object
    • If JSON response is detected, it is automatically converted into a JavaScript object

Part 18 $http service in AngularJS的更多相关文章

  1. Part 17 Consuming ASP NET Web Service in AngularJS using $http

    Here is what we want to do1. Create an ASP.NET Web service. This web service retrieves the data from ...

  2. Part 20 Create custom service in AngularJS

    Whenever the case changes from lower to upper, a single space character should be inserted. This mea ...

  3. AngularJs学习笔记--Managing Service Dependencies

    原版地址:http://docs.angularjs.org/guide/dev_guide.services.managing_dependencies angular允许service将其他ser ...

  4. angularJS(6)

    angularJS(6) 一:angularJs的事件. 1.ng-click指令定义了AngularJS点击事件. <div ng-app="myapp" ng-contr ...

  5. AngularJs之五

    一:angularJs的事件. 1.ng-click指令定义了AngularJS点击事件. <div ng-app="myapp" ng-controller="m ...

  6. Angular Service入门

    1.Angular内置service Angular为了方便开发者开发,本身提供了非常多的内置服务.可以通过https://docs.angularjs.org/api/ng/service查看Ang ...

  7. 前端见微知著AngularJS备忘篇:温故而知新,可以为师矣

    话说以前JQuery刚出来的时候,真的是对个人的冲击蛮大的.记得当时我买的第一本书就是<锋利的JQuery>,藉由这本书开始,我从此以后的项目基本用上了JQuery,其给我带来的便利性是不 ...

  8. 【AngularJS学习笔记】01 指令、服务和过滤器

    AngularJS 指令是扩展的 HTML 属性,带有前缀 ng-. 比如: ng-app 指令初始化一个 AngularJS 应用程序.注意ng-app一般为空,如果值不为空,就得加这样一句代码va ...

  9. AngularJs学习笔记--Forms

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/forms 控件(input.select.textarea)是用户输入数据的一种方式.Form(表单) ...

随机推荐

  1. P4022-[CTSC2012]熟悉的文章【广义SAM,dp,单调队列】

    正题 题目链接:https://www.luogu.com.cn/problem/P4022 题目大意 给出\(m\)个模板串. 然后\(n\)次询问给出一个串\(S\)要求找到一个最大的\(L\)使 ...

  2. P4542-[ZJOI2011]营救皮卡丘【费用流,Floyd】

    正题 题目链接:https://www.luogu.com.cn/problem/P4542 题目大意 给出\(n+1\)个点\(m\)条边的无向图,\(k\)个人开始在\(0\)号点,一个人进入\( ...

  3. Python日常Bug集

    1.TypeError: 'int' object is not iterable: 场景示例: data = 7 for i in data: print(i) # 原因:直接对int数据进行迭代造 ...

  4. 实例:建立图书借阅系统的UML模型

    1.需求分析 图书借阅系统的组成 2.具体的功能详细描述: (1)管理员登录系统,进入借书工作状态,等待借书处理. (2)读者找到所需图书,在借书处上刷卡机上刷卡. (3)管理员对借阅证进行资格审查. ...

  5. 图数据库Neo4j的基本使用及与SpringBoot集成

    Neo4j 官网地址:https://neo4j.com/ 下载地址:https://neo4j.com/download-center/#community 官方入门文档:https://neo4j ...

  6. 在开源项目或项目中使用git建立fork仓库

    前言: vector我们经常使用,对vector里面的基本函数构造函数.增加函数.删除函数.遍历函数我们也会用到.其中在使用遍历之后erase删除元素过程中,会出现一种删除最后一个元素破坏了迭代器的情 ...

  7. Java基础之(三):IDEA的安装及破解

    IDEA的安装 IDEA官网:IDEA 点击IJ 找好操作系统,点击下载 双击打开,自己找个安装路径 勾选这两个即可 旗舰版破解及汉化 上面是个人社区版,是免费的,但是如果想要使用汉化版的,需要寻找插 ...

  8. 洛谷4455 [CQOI2018]社交网络 (有向图矩阵树定理)(学习笔记)

    sro_ptx_orz qwq算是一个套路的记录 对于一个有向图来说 如果你要求一个外向生成树的话,那么如果存在一个\(u\rightarrow v\)的边 那么\(a[u][v]--,a[v][v] ...

  9. IEEE 754 浮点数加减运算

    电子科技大学 - 计算机组成原理 小数的十进制和二进制转换 移码 定义:[X]移 = X + 2n ( -2n ≤ X < 2n ) X为真值,n为整数的位数 数值位和X的补码相同,符号位与补码 ...

  10. 【机器学习基础】逻辑回归——LogisticRegression

    LR算法作为一种比较经典的分类算法,在实际应用和面试中经常受到青睐,虽然在理论方面不是特别复杂,但LR所牵涉的知识点还是比较多的,同时与概率生成模型.神经网络都有着一定的联系,本节就针对这一算法及其所 ...