Posted in Javascript onOctober 30, 2017
本文实例讲述了AngularJS实现controller控制器间共享数据的方法。分享给大家供大家参考,具体如下:
<!DOCTYPE html> <html ng-app="myapp"> <head> <title>3water.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程序设计有所帮助。
AngularJS实现controller控制器间共享数据的方法示例
- Author -
timelessmemoryli声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@