Service and controller in angularJs

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/

时间: 2024-10-01 21:55:02

Service and controller in angularJs的相关文章

Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析

URL:http://www.ulewo.com/user/10001/blog/273 我们在使用spring的时候经常会用到这些注解,那么这些注解到底有什么区别呢.我们先来看代码 同样分三层来看: Action 层: package com.ulewo.ioc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Co

Spring注解@Component、@Repository、@Service、@Controller区别

Spring注解@Component.@Repository.@Service.@Controller区别 Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3 个注释和 @Comp

SpringMVC注解@Component、@Repository、@Service、@Controller区别

SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component.@Repository @Service.@Controller SpringMVC中四个基本注解: @Component.@Repository   @Service.@Controller 看字面含义,很容易却别出其中三个: @Controller   控制层,就是我们的action层 @Service        业务逻辑层,就是我们的service或者manager层 @Repo

@Repository、@Service、@Controller 和 @Component

@Repository .@Service . @Controller .@Component 这四个Spring注解 ,用于把加了注解的 类 加入到Spring 容器中管理,节省了xml 的繁重的配置,尽管如此xml 同样可以实现(一般建议先搞懂xml). @Repository @Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean.具体只需将该注解标注在 DAO类上即可.同时,为了让 Spring 能够扫描类路径中的类并识别

Spring中用@Component、@Repository、@Service和 @Controller等标注的默认Bean名称会是小写开头的非限定类名

今天用调度平台去调用bean中的方法时,提示找不到bean.经查,发现是由于如果在标注上没有提供name属性值,则默认的bean名称是小写开头的,而不是大写开头的. 下面是其他文档参阅: 使用过滤器自定义扫描 <context:component-scan base-package="org.example"> spring开发手册中这样写: 3.12.4. 自动检测组件的命名 当一个组件在某个扫描过程中被自动检测到时,会根据那个扫描器的BeanNameGenerator 

Spring注解@Repository、@Service、@Controller、@Component

继前几章所讲解的注解中: http://www.cnblogs.com/EasonJim/p/6892280.html http://www.cnblogs.com/EasonJim/p/6899747.html http://www.cnblogs.com/EasonJim/p/6901115.html 发现上述遗漏了一些注解,比如:@Repository.@Service.@Controller.@Component,现在做如下补充: Spring自2.0版本开始,陆续引入了一些注解用于简化

Spring学习之使用标签来标记资源(@Component、@Repository、 @Service和@Controller)以及使用方式(包含如何在jsp中使用)

首先要在xml文件当中加入标下划线的部分,容器初始化时候需要扫描的包 注意: a.     扫描的包部分(下划线部分)一定要加上,默认是不会扫描所有的包的.各个包之间用','隔开.如过具有相同的父包,那么我们可以用父包来代替.如下划线部分,我们可以用com.bjsxt来代替. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.or

Spring注解@Component、@Repository、@Service、@Controller的相关知识

Spring注解@Component.@Repository.@Service.@Controller区别 spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3 个注释和 @Comp

Spring注解:@Repository、@Service、@Controller、@Autowired

Spring的注解形式:@Repository.@Service.@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean. 作用 @Repository.@Service.@Controller 和 @Component将类标识为Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发. @Repository 注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean. 具体只需将该注解