神奇的angularJS --连接controller的桥梁 service

一、什么是service

在以前的文章中,我们提到,controller是相对独立的,也就是说,两个controller之间,内存是不共享的,这个controller是无法访问其他其他controller的属性或者方法的。但是,实际项目中,页面1的controller是有可以需要页面2的controller的。以前,我都是通过localStorage来进行储存,后来发来localStorage应该是用来存储持久化数据,用来存储临时数据,controller互相交互,官方建议通过service来进行相互访问。
也就是说,service是用于不同controller或者directive中用于共享数据的一种服务。



二、一个简单的service

angular中,定义service有很多方法,如 constant,factory,service,provider,这次,我们使用service来定义一个简单的service。
[javascript] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. app.service('demoService'function () {  
  2.     "use strict";  
  3.   
  4.     var privateAuthor = {              //私有变量  
  5.         name: 'jack',  
  6.         sex: 'male'  
  7.     }  
  8.   
  9.     this.publicAuthor = {        //共有变量  
  10.         name: 'rose',  
  11.         sex: 'female'  
  12.     }  
  13.   
  14.     this.getPriAuthor = function () {          //获取私有变量  
  15.         return publicAuthor;  
  16.     }  
  17. });  
app.service('demoService', function () {
    "use strict";

    var privateAuthor = {              //私有变量
        name: 'jack',
        sex: 'male'
    }

    this.publicAuthor = {        //共有变量
        name: 'rose',
        sex: 'female'
    }

    this.getPriAuthor = function () {          //获取私有变量
        return publicAuthor;
    }
});
 我们用app.service定义了一个服务,里面有一个私有属性,一个公有属性,以及一个获取私有属性的方法。现在 我们就可以在controller中使用这个service

因此 我们在以前的controller中注册这个service

[javascript] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. <pre style="font-family: 宋体; font-size: 12pt; background-color: rgb(255, 255, 255);">  
<pre style="font-family: 宋体; font-size: 12pt; background-color: rgb(255, 255, 255);">
[javascript] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. app.controller('helloCtrl'function ($scope, demoService) {     //注册service  
  2.     $scope.author = demoService.getPriAuthor();                   //获取私有属性  
  3. });  
  4.   
  5. app.controller('worldCtrl'function ($scope, demoService) {      
  6.     $scope.author = demoService.publicAuthor;                   //获取公有属性  
  7. });  
app.controller('helloCtrl', function ($scope, demoService) {     //注册service
    $scope.author = demoService.getPriAuthor();                   //获取私有属性
});

app.controller('worldCtrl', function ($scope, demoService) {    
    $scope.author = demoService.publicAuthor;                   //获取公有属性
});
 
  
我们在改写index.html,并刷新界面,可以发现已经获取正确的值了
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body ng-app="Hello">  
  8.   
  9. <div style="background: yellow;" ng-controller="worldCtrl">  
  10.     {{author.name}}<br/>  
  11.     {{author.sex}}  
  12. </div>  
  13.   
  14. <div ng-controller="helloCtrl">  
  15.     {{author.name}}<br/>  
  16.     {{author.sex}}  
  17. </div>  
  18.   
  19. </body>  
  20.   
  21. <script src="bower_components/angular/angular.min.js"></script>  
  22. <script src="src/app.js"></script>  
  23. <script src="src/controller.js"></script>  
  24. <script src="src/service.js"></script>  
  25. </html>  
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body ng-app="Hello">

<div style="background: yellow;" ng-controller="worldCtrl">
    {{author.name}}<br/>
    {{author.sex}}
</div>

<div ng-controller="helloCtrl">
    {{author.name}}<br/>
    {{author.sex}}
</div>

</body>

<script src="bower_components/angular/angular.min.js"></script>
<script src="src/app.js"></script>
<script src="src/controller.js"></script>
<script src="src/service.js"></script>
</html>
如图:






三、更改service的值

这时候 我们在controller中更改service的值,再来看看变化。


  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body ng-app="Hello">  
  8.   
  9. <div style="background: yellow;" ng-controller="worldCtrl">  
  10.     {{author.name}}<br/>  
  11.     {{author.sex}}  
  12.     <button ng-click="update()">同步数据</button>  
  13. </div>  
  14.   
  15. <div ng-controller="helloCtrl">  
  16.     {{author.name}}<br/>  
  17.     {{author.sex}}  
  18.     <button  ng-click="updatePublic()">更新公有变量</button>  
  19. </div>  
  20.   
  21. </body>  
  22.   
  23. <script src="bower_components/angular/angular.min.js"></script>  
  24. <script src="src/app.js"></script>  
  25. <script src="src/controller.js"></script>  
  26. <script src="src/service.js"></script>  
  27. </html>  
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body ng-app="Hello">

<div style="background: yellow;" ng-controller="worldCtrl">
    {{author.name}}<br/>
    {{author.sex}}
    <button ng-click="update()">同步数据</button>
</div>

<div ng-controller="helloCtrl">
    {{author.name}}<br/>
    {{author.sex}}
    <button  ng-click="updatePublic()">更新公有变量</button>
</div>

</body>

<script src="bower_components/angular/angular.min.js"></script>
<script src="src/app.js"></script>
<script src="src/controller.js"></script>
<script src="src/service.js"></script>
</html>

  1. app.controller('helloCtrl', function ($scope, demoService) {  
  2.     $scope.author = demoService.publicAuthor;  
  3.     $scope.updatePublic = function () {                              //更新您数据  
  4.         demoService.publicAuthor = {  
  5.             name: 'fei',  
  6.             sex: 'female'  
  7.         }  
  8.         $scope.author = demoService.publicAuthor;  
  9.     }  
  10. });  
  11.   
  12. app.controller('worldCtrl', function ($scope, demoService) {  
  13.     $scope.author = demoService.publicAuthor;  
  14.     $scope.update = function () {                          //同步数据  
  15.         $scope.author = demoService.publicAuthor;  
  16.     }  
  17. });  
app.controller('helloCtrl', function ($scope, demoService) {
    $scope.author = demoService.publicAuthor;
    $scope.updatePublic = function () {                              //更新您数据
        demoService.publicAuthor = {
            name: 'fei',
            sex: 'female'
        }
        $scope.author = demoService.publicAuthor;
    }
});

app.controller('worldCtrl', function ($scope, demoService) {
    $scope.author = demoService.publicAuthor;
    $scope.update = function () {                          //同步数据
        $scope.author = demoService.publicAuthor;
    }
});
我们点击更新数据后,我们会更改 demoService.publicAuthor的值,我们在点同步数据 ,会发现 两个controller中的值进行了同步。
更新




同步:



小结

我们发现service的作用就是用于数据共享,将同一个service中的内容分给不同的controller,通过官方我们知道controller有很多创建service的方法,我们将会在下一章进行讲解。

转自:http://blog.csdn.net/ft6302244/article/details/42640073
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值