AngularJS如何实现controller控制器间共享数据

2023-05-21,,

这篇文章主要介绍了AngularJS如何实现controller控制器间共享数据,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

具体如下:

<!DOCTYPE html>
<html ng-app="myapp">
<head>
<title>www.jb51.net 控制器间共享数据</title>
  <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="external nofollow" > 
  <style>
    body { 
      padding-top:30px; 
    }
  </style>
  <script src="angular.min.js"></script> 
  <script>
    angular.module('myapp', [])
    .factory("Data", function() {
      return {
        msg:"hello world"
      };
    })
    .controller("CtrlF", function($scope, Data) {
      $scope.data = Data;
    })
    .controller("CtrlS", function($scope, Data) {
      $scope.data = Data;
    });
  </script>
</head>
<body>
<div class="container">
<div class="col-sm-8 col-sm-offset-2">
<div ng-controller="CtrlF">
  <input type="text" ng-model="data.msg">
  <h2>{{data.msg}}</h2>
</div>
<div ng-controller="CtrlS">
  <input type="text" ng-model="data.msg">
  <h2>{{data.msg}}</h2>
</div>
</div><!-- col-sm-8 -->
</div><!-- /container -->
</body>
</html>

运行效果:

感谢你能够认真阅读完这篇文章,希望小编分享的“AngularJS如何实现controller控制器间共享数据”这篇文章对大家有帮助,同时也希望大家多多支持本站,关注本站行业资讯频道,更多相关知识等着你来学习!

《AngularJS如何实现controller控制器间共享数据.doc》

下载本文的Word格式文档,以方便收藏与打印。