angularjs 中使用 service 在controller 之间 share 对象和数据
在做angularjs 的UI 时,我们经常会遇到一个页面之间有几个controller,在controller 之间share 公共的一些数据和方法就变得比较困难,目前推荐的做法是创建一个service, 在service 中存储公共的数据,然后把service 注入到controller中来达到share 数据的目的。
下面是最简单的一个sample 列子
angularjs 模板页面, 有userContoller 和 customerController,我们将在这两个controller 之间共享数据, 具体定义如下:
<div ng-app="APP">
<h2>angularjs sample</h2>
<div ng-controller="userController">
<div>{{golbal_sitename}}</div>
<div>{{controllerName}}</div>
<div><button ng-click="sayHello()">sayHello</button></div>
</div>
<hr />
<div ng-controller="customerController">
<div>{{golbal_sitename}}</div>
<div>{{controllerName}}</div>
<div><button ng-click="sayHello()">sayHello</button></div>
</div>
</div>
angularjs js 页面, 在这个代码中我们定义了 module APP, userController 和customerController, 另外我们还定义了一个service, dataService,这个service 包含需要共享的数据和方法,在这里我们返回了一个属性golbal_sitename, 和 一个sayHello 方法。
然后,在声明controller 的时候,我们把dataservice 这个对象分别注入到userController 和customerController,注入完成后,我们就可以在controller 的代码中访问dataService共享的数据了。
var APP = angular.module('APP',[]).
controller('userController', ['$scope','dataService',function($scope,dataService) {
$scope.controllerName='userController';
$scope.golbal_sitename=dataService.golbal_sitename;
$scope.sayHello =function(){
dataService.sayHello($scope.controllerName);
}
}]).
controller('customerController',['$scope','dataService', function($scope,dataService) {
$scope.controllerName='customerController';
$scope.golbal_sitename=dataService.golbal_sitename;
$scope.sayHello =function(){
dataService.sayHello($scope.controllerName);
}
}]).
factory('dataService',function(){
return {
golbal_sitename:"this is the shared value",
sayHello:function(msg){
alert(msg);
}
}
});
最后的结果截图如下:

从图中我们可以看到
angularjs 中使用 service 在controller 之间 share 对象和数据的更多相关文章
- angularjs 中 Factory,Service,Provider 之间的区别
本片文章是使用了 angularjs 中使用 service 在controller 之间 share 对象和数据 的code(http://jsfiddle.net/kn46u0uj/1/) 来进行 ...
- AngularJS中使用Directive、Controller、Service
AngularJS是一款非常强大的前端MVC框架.同时,它也引入了相当多的概念,这些概念我们可能不是太熟悉. (1)Directive 指令 (2)Controller 控制器 (3)Service ...
- AngularJS中使用service,并同步数据
service是单例对象,在应用中不同代码块之间共享数据. 对一些公用的方法封装到service中,然后通过依赖注入在Controller中调用,示例代码: 1.创建一个模块: var module ...
- angularjs中factory, service和provider
在Angular里面,services作为单例对象在需要到的时候被创建,只有在应用生命周期结束的时候(关闭浏览器)才会被清除.而controllers在不需要的时候就会被销毁了(因为service的底 ...
- spring mvc中的service和controller中读取不到properties值
根据web.xml读取配置文件中的顺序来看 controller层和service层来自于spring mvc.xml中读取,所以必须要在spring mvc.xml中配置读取资源文件夹方式
- angularJs 中ui-router 路由向controller传递数据
页面上 : ui-sref="home.dataAnalysis({role:'thirdpart:tokenverify',menuType:'a'})" 路由设置 .state ...
- 【AngularJS中的自定义服务service VS factory VS provider】---它们的区别,你知道么?
在介绍AngularJS自定义服务之前,我们先来了解一下AngularJS~ 学过HTML的人都知道,HTML是一门很好的伪静态文本展示设计的声明式语言,但是,要构建WEB应用的话它就显得乏力了. 而 ...
- Spring Boot 2 实践记录之 封装依赖及尽可能不创建静态方法以避免在 Service 和 Controller 的单元测试中使用 Powermock
在前面的文章中(Spring Boot 2 实践记录之 Powermock 和 SpringBootTest)提到了使用 Powermock 结合 SpringBootTest.WebMvcTest ...
- AngularJS 中 Provider 的用法及区别
在一个分层良好的 Angular 应用中,Controller 这一层应该很薄.也就是说,应用里大部分的业务逻辑和持久化数据都应该放在 Service 里. 为此,理解 AngularJS 中的几个 ...
随机推荐
- cent OS 7 下安装 python 3.6
step1:安装依赖环境 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readli ...
- [STemWin教程入门篇] 第一期:emWin介绍
转自:http://bbs.armfly.com/read.php?tid=1544 SEGGER公司介绍 了解emWin之前,先了解一下SEGGER这家公司,了解生产商才能对emWin有更加全面的认 ...
- HDU 6242 Geometry Problem(计算几何 + 随机化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6242 思路:当 n == 1 时 任取一点 p 作为圆心即可. n >= 2 && ...
- 07、python的基础-->数据类型、集合、深浅copy
一.数据类型 1.列表 lis = [11, 22, 33, 44, 55] for i in range(len(lis)): print(i) # i = 0 i = 1 i = 2 del li ...
- zabbix添加对web页面url的状态监控
zabbix3.0.4添加对web页面url的状态监控 1.应用集配置 在配置—>主机中打开主机列表,选择需要添加监控主机的web,创建应用集 2.web监测配置 选择web场景,再单击右上角的 ...
- Android开发之程序猿必需要懂得Android的重要设计理念
前几天去參加了带着自己的作品去參加服务外包大赛,由于签位抽到的比較靠后就等待了蛮久,就跟坐在前面的一起參赛的选手開始讨论Android的开发经验.各自给对方展示了自己的作品,小伙伴就建议我看 ...
- io.lettuce.core.RedisCommandExecutionException: ERR unknown command 'GEOADD'
io.lettuce.core.RedisCommandExecutionException: ERR unknown command 'GEOADD' at io.lettuce.core.Exce ...
- Android 中MediaPlayer播放音频
1.播放/res/raw/目录下的音频资源 public class MainActivity extends Activity { MediaPlayer mediaPlayer; int posi ...
- node升级
1. 全局安装n: npm i -g n; 2.查看n版本: n -v 3.查看node版本: node -v 4.安装node最新稳定版本: n stable 5.安装node指定版本: n 9.1 ...
- webpack 集成 Typescript && Less
webpack 集成 Typescript && Less TypeScript是JavaScript的一个类型化的超集,可以编译成纯JavaScript,在本指南中,我们将学习如何将 ...