在做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);
}
}
});

最后的结果截图如下:

从图中我们可以看到

”this is the shared value“ 值来自dataService
sayHello 的具体实现也是在dataService中定义的。
 
这样我们就实现了在controller 之间共享对象。
 

angularjs 中使用 service 在controller 之间 share 对象和数据的更多相关文章

  1. angularjs 中 Factory,Service,Provider 之间的区别

    本片文章是使用了 angularjs 中使用 service 在controller 之间 share 对象和数据 的code(http://jsfiddle.net/kn46u0uj/1/) 来进行 ...

  2. AngularJS中使用Directive、Controller、Service

    AngularJS是一款非常强大的前端MVC框架.同时,它也引入了相当多的概念,这些概念我们可能不是太熟悉. (1)Directive 指令 (2)Controller 控制器 (3)Service ...

  3. AngularJS中使用service,并同步数据

    service是单例对象,在应用中不同代码块之间共享数据. 对一些公用的方法封装到service中,然后通过依赖注入在Controller中调用,示例代码: 1.创建一个模块: var module ...

  4. angularjs中factory, service和provider

    在Angular里面,services作为单例对象在需要到的时候被创建,只有在应用生命周期结束的时候(关闭浏览器)才会被清除.而controllers在不需要的时候就会被销毁了(因为service的底 ...

  5. spring mvc中的service和controller中读取不到properties值

    根据web.xml读取配置文件中的顺序来看 controller层和service层来自于spring mvc.xml中读取,所以必须要在spring mvc.xml中配置读取资源文件夹方式

  6. angularJs 中ui-router 路由向controller传递数据

    页面上 : ui-sref="home.dataAnalysis({role:'thirdpart:tokenverify',menuType:'a'})" 路由设置 .state ...

  7. 【AngularJS中的自定义服务service VS factory VS provider】---它们的区别,你知道么?

    在介绍AngularJS自定义服务之前,我们先来了解一下AngularJS~ 学过HTML的人都知道,HTML是一门很好的伪静态文本展示设计的声明式语言,但是,要构建WEB应用的话它就显得乏力了. 而 ...

  8. Spring Boot 2 实践记录之 封装依赖及尽可能不创建静态方法以避免在 Service 和 Controller 的单元测试中使用 Powermock

    在前面的文章中(Spring Boot 2 实践记录之 Powermock 和 SpringBootTest)提到了使用 Powermock 结合 SpringBootTest.WebMvcTest ...

  9. AngularJS 中 Provider 的用法及区别

    在一个分层良好的 Angular 应用中,Controller 这一层应该很薄.也就是说,应用里大部分的业务逻辑和持久化数据都应该放在 Service 里. 为此,理解 AngularJS 中的几个 ...

随机推荐

  1. 【Flutter学习】组件通信(父子、兄弟)

    一,概述 flutter一个重要的特性就是组件化.组件分为两种状态,一种是StatefulWidget有状态组件,一种是StatelessWidget无状态组件. 无状态组件不能更新状态,有状态组件具 ...

  2. sql 基础语法 alter用法和视图,透视

    --查询没有被删除的学生 alter table StuInfo --修改列属性 alter column isdelete bit null alter table StuInfo --删除列 dr ...

  3. layer icon对应图标

    layer icon对应图标 信息框(msg.alert.open.confirm) icon:0 icon:1 icon:2 icon:3 icon:4 icon:5 icon:6 icon:16 ...

  4. BZOJ 3622: 已经没有什么好害怕的了(二项式反演)

    传送门 解题思路 首先将\(a\),\(b\)排序,然后可以算出\(t(i)\),表示\(a(i)\)比多少个\(b(i)\)大,根据容斥套路,设\(f(k)\)表示恰好有\(k\)个\(a(i)\) ...

  5. python 网络编程:socket

    在学习socket之前,我们先复习下相关的网络知识. OSI七层模型:应用层,表示层,会话层,传输层,网络层,数据链路层,物理层.OSI七层模型是由国际标准化组织ISO定义的网络的基本结构,不仅包括一 ...

  6. upc组队赛2 Super-palindrome【暴力枚举】

    Super-palindrome 题目描述 You are given a string that is consisted of lowercase English alphabet. You ar ...

  7. jQuery层次选择器再探究(原创)

    关于层次选择器的详解: 1)可以选取某一个元素的所有的后代元素,得到一个jQuery对象的集合--->$('prev descendant') 2)可以选取某一个元素的子辈的所有的元素,得到一个 ...

  8. UML指南系列——活动图

    活动图用来描述传统意义上的流程图

  9. yum安装LAMP

    安装LAMP环境二进制包安装,先更新yum源,PHP 7.0.33 扩展可选 yum -y install mysql mysql-server mysql-devel httpd httpd-dev ...

  10. ORACLE中的varchar2()与nvarchar2()的讲解

    Oracle中NVARCHAR2和VARCHAR2的区别 [转]NVARCHAR2和VARCHAR2的区别,从使用角度来看区别在于:NVARCHAR2在计算长度时和字符集相关的,例如数据库是中文字符集 ...