AngularJS实现controller控制器间共享数据的方法示例
timelessmemoryli 人气:0本文实例讲述了AngularJS实现controller控制器间共享数据的方法。分享给大家供大家参考,具体如下:
<!DOCTYPE html> <html ng-app="myapp"> <head> <title>www.qb5200.com 控制器间共享数据</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"> <h1>{{data.msg}}</h1> </div> <div ng-controller="CtrlS"> <input type="text" ng-model="data.msg"> <h1>{{data.msg}}</h1> </div> </div><!-- col-sm-8 --> </div><!-- /container --> </body> </html>
运行效果:
希望本文所述对大家AngularJS程序设计有所帮助。
加载全部内容