Services 在多个 controller 中共享数据。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible"content="IE=edge" />
<meta name="viewport"content="width=device-width,initial-scale=1" />
<title></title>
<meta charset="utf-8" />
<link rel="stylesheet"href="bootstrap-3.3.5/dist/css/bootstrap.min.css" />
</head>
<body data-ng-app="myApp">
<div class="container"style="padding:50px">
<div data-ng-controller="fCtrl">
<input type="text"data-ng-model="data.msg" />
<p>{{data.msg}}</p>
</div>
<div data-ng-controller="sCtrl">
<input type="text"data-ng-model="data.msg" />
<p>{{data.msg}}</p>
</div>
</div>
<script src="jQuery/jquery-2.1.4.js"></script>
<script src="bootstrap-3.3.5/dist/js/bootstrap.min.js"></script>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="js/app1.js"></script>
</body>
</html>
app1.js
angular.module("myApp", [])
.factory("Data", function () {
return {
msg:"我来自factory"
}
})
.controller("fCtrl", function ($scope,Data) {
$scope.data = Data;
})
.controller("sCtrl", function ($scope,Data) {
$scope.data = Data;
})