angular service讲解
controller是相对独立的,也就是说,两个controller之间,内存是不共享的,这个controller是无法访问其他其他controller的属性或者方法的;
<!-- 一个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讲解的更多相关文章
- Angular Service入门
1.Angular内置service Angular为了方便开发者开发,本身提供了非常多的内置服务.可以通过https://docs.angularjs.org/api/ng/service查看Ang ...
- angular service provider
关于 angular service factory provider 方面有很多,我也来写一篇加深下印象 provider 是一切方法的基础,所以功能也最强,provider 用来定义一个可以被 ...
- thinkphp模型层Model、Logic、Service讲解
thinkphp模型层Model.Logic.Service讲解 时间:2014-08-24 15:54:56 编辑:一切随缘 文章来源:php教程网 已阅读:771 次 js特效 ...
- Angular service, 服务
早上开车上班, 发现车快没油了, 于是拐进加油站. 有一辆出租车也在加油.. Angular service在一个应用里是以单例形式存在的. 这个单例的实例是由service factory( ...
- AngularJS学习之 ngTable 翻页 功能以及利用angular service准备测试数据
1.官网链接 https://github.com/esvit/ng-table#4.0.0 2.安装ngTable后,一定要记得先注册到自己的项目 .module('pttengApp', [ ' ...
- angular service/directive
<html class=" js cssanimations csstransitions" ng-app="phonecatApp" > < ...
- Angular service定义服务
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...
- [Angular] Service Worker Version Management
If our PWA application has a new version including some fixes and new features. By default, when you ...
- 第14 章 : Kubernetes Service讲解
Kubernetes Service 本文将主要分享以下四方面的内容: 为什么需要 K8s service: K8s service 用例解读: K8s service 操作演示: K8s servi ...
随机推荐
- Studio-----快捷键大全
Ctrl+Alt+Space 类名或接口名提示; 补充布局的提示: 26. Ctrl+Alt+Space是类名自动完成 Ctrl+X 删除行 Ctrl+D 复制行 Alt+回车 导入包,自动修正 Cr ...
- ScrollView--嵌套GridView的解决办法
前些日子在开发中用到了需要ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全. 解决办法,自定义一个GridVie ...
- 转--一款漂亮实用的Android开源日期控件timessquare
这个开源控件可以兼容到SDK8版本,可以自定义显示的年月日,以及时间范围,如图 如果我们只想显示两个月的日期选择区间: final Calendar month = Calendar.getInsta ...
- Ubuntu离线安装包制作(转载)
From:http://blog.csdn.net/nupt123456789/article/details/11649603 1.应用场景 a.需要在多台电脑上安装同一软件,且软件很大,下载需要时 ...
- 30天轻松学习javaweb_Eclipse在修改了web.xml后将自动更新到tomcat服务器中
context.xml中增加<WatchedResource>WEB-INF/web.xml</WatchedResource>,Eclipse在修改了web.xml后将自动更 ...
- R %operator% 含义
%foo% is the syntax for a binary operator. In base R: %in%: '"%in%" <- function(x, tabl ...
- nbIoT基础概念
1. 物理信道(L1与L2之间) 上行:PRACH.PUSCH 下行:PBCH.PDCCH.PDSCH 2.逻辑信道(L2与L3之间) CCCH.DCCH.DTCH 3.信令(L3与NAS层之间) D ...
- 认识与学习BASH(中)
1.在设置变量中:单引号与双引号的最大不同:双引号能保有变量的内容,单引号仅能是一般字符 2.反单引号(`)作用:在一串指令中,在‘之内的指令将会被先执行,其结果将作为外部的输入信息. locate指 ...
- java异常处理机制throw
- Plan9 与 Plan9port
Plan9 Plan9 是一个操作系统.由贝尔实验室开发的,其主要的负责人是Rob Pike(现在在google工作,负责Go语言的开发). 参考:http://www.cnblogs.com/yjf ...