controller是相对独立的,也就是说,两个controller之间,内存是不共享的,这个controller是无法访问其他其他controller的属性或者方法的;

以前,我都是通过localStorage来进行储存,后来发来localStorage应该是用来存储持久化数据,用来存储临时数据,controller互相交互,官方建议通过service来进行相互访问。
 
也就是说,service是用于不同controller或者directive中用于共享数据的一种服务。
 
举例说明:
html
<!-- 一个controller -->
<div style="background: yellow;" ng-controller="worldCtrl">
{{author.name}}
<br/> {{author.sex}}
<button ng-click="update()">同步数据</button>
</div> <!-- 另一个controller -->
<div ng-controller="helloCtrl">
{{author.name}}
<br/> {{author.sex}}
<button ng-click="updatePublic()">更新公有变量</button>
</div>

controller.js

var app = angular.module('Hello', []);

app.controller('worldCtrl', function($scope, demoService) {
$scope.author = demoService.publicAuthor; $scope.update = function() { //同步数据
$scope.author = demoService.publicAuthor;
}
}); app.controller('helloCtrl', function($scope, demoService) {
$scope.author = demoService.publicAuthor; $scope.updatePublic = function() { //更新您数据
demoService.publicAuthor = {
name: 'fei',
sex: 'female'
}
$scope.author = demoService.publicAuthor;
}
});

service.js

app.service('demoService', function () {
var privateAuthor = { //私有变量
name: 'jack',
sex: 'male'
} this.publicAuthor = { //共有变量
name: 'rose',
sex: 'female'
} this.getPriAuthor = function () { //获取私有变量
return publicAuthor;
}
});

end.

angular service讲解的更多相关文章

  1. Angular Service入门

    1.Angular内置service Angular为了方便开发者开发,本身提供了非常多的内置服务.可以通过https://docs.angularjs.org/api/ng/service查看Ang ...

  2. angular service provider

    关于  angular service factory  provider 方面有很多,我也来写一篇加深下印象 provider 是一切方法的基础,所以功能也最强,provider 用来定义一个可以被 ...

  3. thinkphp模型层Model、Logic、Service讲解

    thinkphp模型层Model.Logic.Service讲解 时间:2014-08-24 15:54:56   编辑:一切随缘   文章来源:php教程网 已阅读:771 次       js特效 ...

  4. Angular service, 服务

      早上开车上班, 发现车快没油了, 于是拐进加油站. 有一辆出租车也在加油..   Angular service在一个应用里是以单例形式存在的. 这个单例的实例是由service factory( ...

  5. AngularJS学习之 ngTable 翻页 功能以及利用angular service准备测试数据

    1.官网链接  https://github.com/esvit/ng-table#4.0.0 2.安装ngTable后,一定要记得先注册到自己的项目 .module('pttengApp', [ ' ...

  6. angular service/directive

    <html class=" js cssanimations csstransitions" ng-app="phonecatApp" > < ...

  7. Angular service定义服务

    <!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...

  8. [Angular] Service Worker Version Management

    If our PWA application has a new version including some fixes and new features. By default, when you ...

  9. 第14 章 : Kubernetes Service讲解

    Kubernetes Service 本文将主要分享以下四方面的内容: 为什么需要 K8s service: K8s service 用例解读: K8s service 操作演示: K8s servi ...

随机推荐

  1. POJ 3311 Hie with the Pie(Floyd+状态压缩DP)

    题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...

  2. Standing on Shouder of Giants

    Zachary_Fan 如何一步一步用DDD设计一个电商网站(二)-- 项目架构 http://www.cnblogs.com/Zachary-Fan/p/6012454.html HTTP 权威指南 ...

  3. poj 3040 Allowance

    Allowance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1842   Accepted: 763 Descript ...

  4. SQL Server 2008 建立分区表 脚本

    /*第一步:创建分区函数*/Create partition function Part_func_Bag(varchar(20)) as range right /*正式区间for values(N ...

  5. (英文版)使用Visual Studio 2015 编写 MASM 汇编程序!

    原文地址:http://kipirvine.com/asm/gettingStartedVS2015/index.htm#CreatingProject Getting Started with MA ...

  6. Vim 中截取部分内容保存到其他文件

    最近无聊,突然想跟着玩玩天池数据挖掘,发现数据好大,想转换到mysql数据库,phpmyadmin import 导入时抱错! 数据文件大大! 于是乎,准备截取一小段到另外一个文件测试先,然后,发现了 ...

  7. mysql 增加用户

    mysql 增加用户 3.增加用户: (注意:和上面不同,下面的因为是MYSQL环境中的命令,所以后面都带一个分号作为命令结束符) 格式:grant select on 数据库.* to 用户名@登录 ...

  8. ruby api 2.1新增改变

    -> 这个符号可以替换lambda%i 生成符号数组 %i(foor bar baz) #[:foo,:bar:baz]def 定义方法 eg: def foo(x: 1); puts; end ...

  9. linux内核设计与实现--从内核出发

    linux内核有两种版本:稳定的和处于开发中的. linux通过一种简单的命名机制来区分稳定的和处于开发中的内核,使用3个或者4个“.”分割的数字来代表不同内核版本. 如:2.6.26.1:第一个数字 ...

  10. JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method

    在使用Java局部内部类或者内部类时,若该类调用了所在方法的局部变量,则该局部变量必须使用final关键字来修饰,否则将会出现编译错误“Cannot refer to a non-final vari ...