本文实例讲述了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"> <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相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》
希望本文所述对大家AngularJS程序设计有所帮助。