码迷,mamicode.com
首页 > Web开发 > 详细

Service and controller in angularJs

时间:2014-12-04 19:37:03      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   sp   for   strong   on   

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/

 

Service and controller in angularJs

标签:des   blog   http   io   ar   sp   for   strong   on   

原文地址:http://www.cnblogs.com/jiayouwyhit/p/4143401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!