Part 19 AngularJS Services
What is a service in AngularJS
Before we talk about what a service is in Angular. Let's talk about a service in web development.
If you have any experience developing web applications
1. You might have heard about Web Services and WCF Services
2. You might have also created objects that provide some services. For example, a Math object may provide services to add numbers.
So, a service in Angular is simply an object that provide some sort of service that can be reused with in an angular application. The angular service object has properties and methods, just like any other JavaScript object.
AngularJS has lot of built in services. We discussed two of the built in services - $http & $log, in our previous video. $http service is used to make AJAX calls. $log service is useful to log an object to the console, which is very useful when debugging applications. We can also create our own custom services, which we will discuss in a later video.
For now let's understand the need for services.
Why do we need services in an angular application
The primary responsibility of the controller is to build the model for the view. The controller should not be doing too many things. For example, if the controller also has the logic to compute Age from Date of Birth, it violates one of the SOLID principles i.e the Single Responsibility Principle. The Single Responsibility Principle states that an object should only have a Single Responsibility. So this kind a logic belongs in it's own service, which can then be injected into the object that needs that service.
In our previous video session, we have injected 2 of the angular built in services i.e $http and $log service into the controller function that needs them.
In general, if the logic with in your controller, is becoming too large or too complex, then it is time, to take a step back, and think if anything can be abstracted into it's own service.
Services can be used by controllers, directives and filters.
What are the benefits of using services
Reusability : In a service you usually have a logic that you want to reuse with in your entire application. For example, any time you want to make AJAX calls, you can use one of the built in angular service - $http, simply by injecting it into the object that needs that service. The application is also easier to maintain when the reusable components are encapsulated into their own services.
Dependency Injection : Another benefit of services, is that, they can simply be injected into controllers or other services that need them.
Testability : Since services are injected into controllers or other services that need them, it becomes very easy to test them. Depending on which service you are testing, you can pass mock implementations or real implementations.
Part 19 AngularJS Services的更多相关文章
- [译]AngularJS Services 获取后端数据
原文:ANGULARJS SERVICES – FETCHING SERVER DATA $http是AngularJS内置的服务,能帮助我们完成从服务端获数据.简单的用法就是在你需要数据的时候,发起 ...
- AngularJS笔记---数据绑定
一.数据绑定 1.简单绑定 下面实现了一个简单的加法运算的绑定, A.ng-app:表示该div以内都在AngularJS的应用, 去掉ng-app="" 那么后面的绑定都将无效 ...
- [AngularJS] Services, Factories, and Providers -- value & Providers
Creating a Value Object Sometimes you have javascript object defined: //value object var droidValue ...
- [AngularJS] Services, Factories, and Providers -- Service vs Factory
Creating a Service: Before actual create an angular service, first create a constructor in Javascrip ...
- angularjs跨域调取webservice
1.配置 web.config <webServices> <!--必须添加--> <protocols> <add name="HttpGet&q ...
- [译]AngularJS sercies - 获取后端数据
原文:ANGULARJS SERVICES – FETCHING SERVER DATA $http是AngularJS内置的服务,能帮助我们完成从服务端获数据.简单的用法就是在你需要数据的时候,发起 ...
- [译]用AngularJS构建大型ASP.NET单页应用(三)
原文地址:http://www.codeproject.com/Articles/808213/Developing-a-Large-Scale-Application-with-a-Single A ...
- angularjs+jasmine单元测试入门
使用cordova.angularjs.ionic开发hybrid App有一段时间了.为了做单元测试,之前一直是把要测的某一部分产品代码复制到另一个单独的工程中来写测试代码,测好了以后再复制回去.弊 ...
- angularjs编码实践
AngularJS 是制作 SPA(单页面应用程序)和其它动态Web应用最广泛使用的框架之一.我认为程序员在使用AngularJS编码时有一个大的列表点应该记住,它会以这样或那样的方式帮助到你.下面是 ...
随机推荐
- 吴恩达-神经网络-week1-hw3
Ref:https://blog.csdn.net/u013733326/article/details/79702148 点击查看代码 from testCases import * from pl ...
- webstorm 修改端口号
webstorm 修改端口号: 至此,点击下方 [apply],端口号修改完成.
- electron-builder进行DEBUG输出的正确方式
前言 使用Electron进行打包通常会用到electron-builder或者electron-packager两种工具.在使用electron-builder的时候,由于对机制的不熟悉,我们在打包 ...
- WEB 标准以及 W3C 的理解与认识
01. WEB标准 ① web标准 简单来说可以分为结构.表现和行为. ② 结构:主要是有HTML标签组成(通俗点说,在页面body里面我们写入的标签都是为了页面的结构) 表现:即指css样 ...
- Data Interoperability Tools
这里的工具貌似没有对应函数~~~
- NOIP模拟77
前言 感觉最近太飘了,这次考试是挺好的一次打击(好像也不算是). 犯了一个智障错误(双向边一倍数组 100pts->30pts)别的就.. T1 最大或 解题思路 一开始我以为是一个找规律,然而 ...
- TCP三次握手四次挥手,通俗易懂版
三次握手四次挥手 三次握手 其实很好理解,三次握手就是保证双手都有发送和接受的能力.那么最少三次才能验证完成 即----> 客户端发送---服务端收到----服务端发送-- 1.客户端发送 -- ...
- C程序内存布局
作为计算机专业的来说,程序入门基本都是从C语言开始的,了解C程序中的内存布局,对我们了解整个程序运行,分析程序出错原因,会起到事半功倍的作用 . C程序的内存布局包含五个段,分别是STACK(栈段), ...
- 每日一题,是否存在(c语言)
每日一题:1.是否存在 是否存在描述猫咪非常喜欢饼干,尤其是字母饼干.现在,她得到一些字母饼干,她希望选择他们中的一些拼写某些单词. 你的任务是确定她是否可以拼出自己想要的单词. 输入输入包含若干测试 ...
- programmercarl——数组——二分查找
二分查找,在经过: 34--https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-arr ...