Service and controller in angularJs
2014-12-04 18:01
447 查看
Separation of concern is at the heart while designing an AngularJS application. Your controller must be responsible for binding model data to views using $scope. It does not contain logic to fetch the data or manipulating it.
For that we must create singleton objects called services. AngularJS can manage these service objects. Wherever we want to use the service, we just have to specify its name and AngularJS auto-magically inject these objects (more on this later).
Thus service is a stateless object that contains some useful functions. These functions can be called from anywhere; Controllers, Directive, Filters etc. Thus we can divide our application in logical units. The business logic or logic to call HTTP url to fetch data from server can be put within a service object.
Putting business and other logic within services has many advantages. First it fulfills the principle of separation of concern or segregation of duties. Each component is responsible for its own work making application more manageable. Second this way each component can be more testable. AngularJS provides first class support for unit testing. Thus we can quickly write tests for our services making them robust and less error prone.
And I must say that reference [1] is really a good article!
Reference
[1]http://viralpatel.net/blogs/angularjs-service-factory-tutorial/
For that we must create singleton objects called services. AngularJS can manage these service objects. Wherever we want to use the service, we just have to specify its name and AngularJS auto-magically inject these objects (more on this later).
Thus service is a stateless object that contains some useful functions. These functions can be called from anywhere; Controllers, Directive, Filters etc. Thus we can divide our application in logical units. The business logic or logic to call HTTP url to fetch data from server can be put within a service object.
Putting business and other logic within services has many advantages. First it fulfills the principle of separation of concern or segregation of duties. Each component is responsible for its own work making application more manageable. Second this way each component can be more testable. AngularJS provides first class support for unit testing. Thus we can quickly write tests for our services making them robust and less error prone.
And I must say that reference [1] is really a good article!
Reference
[1]http://viralpatel.net/blogs/angularjs-service-factory-tutorial/
相关文章推荐
- Code Organization in Large AngularJS and JavaScrip
- 我也谈“the difference between Factory, Service, and Provider in Angular”
- ASP.NET Web API 2 external logins with Facebook and Google in AngularJS app
- How to call a service function in AngularJS ng-click
- Part 20 Create custom service in AngularJS
- 理解 AngularJS $q service and promises
- Animating CSS in AngularJS and Angular 2
- Compile, Pre, and Post Linking in AngularJS
- Part 17 Consuming ASP NET Web Service in AngularJS using $http
- angular this vs $scope (How does 'this' and $scope work in AngularJS controllers?)
- angularJs-The factory and service of angularJs
- How to use $http and $resource in Angular JS
- Dynamically Loading Controllers and Views with AngularJS/$controllerProvider and RequireJS
- AngularJS + ui-router + RequireJS异步加载注册controller/directive/filter/service
- Part 35 AngularJS caseInsensitiveMatch and Inline Templates
- Working with Validators and Messages in AngularJS
- [Angular 2] Managing State in RxJS with StartWith and Scan
- angularjs controller, service, directive 的demo
- AngularJS Service vs Factory - Once and for all
- Angularjs accessing methods in another factory/service