angular.module('myApp',[]).factory('Data',function () { // this.$get = function(){} return { message : '共享数据' } }) .controller('firstController',function ($scope,Data) { //第一种方法 $scope.data = { name : '' } //第二种方法 $scope.Data = Data; }) .controller('secondController',Data) { //第一种方法 $scope.data = $scope.$$prevSibling.data; //第二种方法 $scope.Data = Data; })调用:
<!DOCTYPE html> <html lang="en"> <head> <Meta charset="UTF-8"> <title>Title</title> </head> <body> <div ng-app="myApp"> <div ng-controller="firstController"> first.data<input type="text" ng-model="data.name"> first.Data<input type="text" ng-model="Data.massage"> <p> first:{{data.name}} </p> <p> first:{{Data.massage}} </p> </div> <div ng-controller="secondController"> <p> second:{{data.name}} </p> <p> second:{{Data.massage}} </p> </div> </div> <script type="text/javascript" src="../vendor/angular/angularjs.js"></script> <script type="text/javascript" src="./app/index.js"></script> </body> </html>效果图:
原文链接:https://www.f2er.com/angularjs/145961.html