我們正在構建什麼將成爲一個大角度的應用程序。我們已經使用了大量的指令和服務,並有多達14個控制器。同步範圍在angularjs
我們的問題是在控制器之間共享數據。我們希望能夠在遙遠的控制器(不是兄弟姐妹)之間共享數據,並且不會破壞雙向綁定。
我建議使用專用於攜帶數據的服務。
var MyApp = angular.module('MyApp', []);
MyApp.factory('dataContainer', function(){ return {} });
function FirstCtrl($scope, dataContainer) {
$scope.data = dataContainer;
}
function SecondCtrl($scope, dataContainer) {
$scope.data = dataContainer;
}
你可以在小提琴http://jsfiddle.net/didier_/R2Bgs/2/試試。
這是很好的做法嗎?
請看看這個SO問題http://stackoverflow.com/questions/11252780/whats-the-correct-way-to-communicate-between-controllers-in-angularjs – Chandermani 2013-03-07 13:18:40