Sharing Data Between Controllers
by shriek
HTML
<div ng-app="myApp">
<p ng-controller="FirstCtrl">
{{ data.message }}
</p>
<p ng-controller="SecondCtrl">
{{ data.message }}
</p>
</div>
JavaScript
//aka myApp service
var myApp = angular.module("myApp", []); //[] is dependcies
myApp.factory('Data', function () { //factory creates a service
return {
"message": "I'm data from a service"
};
});
function FirstCtrl($scope, Data) {
$scope.data = Data;
}
function SecondCtrl($scope, Data) {
$scope.data = Data;
}