本文實例講述了AngularJS實現controller控制器間共享數據的方法。分享給大家供大家參考,具體如下:
<!DOCTYPE html><html ng-app="myapp"><head><title>www.49028c.com 控制器間共享數據</title> <link rel="stylesheet" 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程序設計有所幫助。
新聞熱點
疑難解答