I'm trying to create a shared resource between two controllers in angularjs. I have use the getData() in second controller to get the stored data and fill up a data table inside the second controller. I omitted the data Table part in the code for simplicity.
The following is my controller file with two controllers
(function(){
var app = angular.module('adminCtrls',[]);
// Main Controller
app.controller('mainCtrl', function( $scope, $http,testSrv ){
// Get the data form the phoenix database based on the query defined
// in db_size.php
$http.get('rest/admin/db_size.php').then(function(response){
$scope.answers = response.data;
$scope.me = "$scope.answer";
testSrv.setData($scope.me);
});
});
})
// Modal Controller
app.controller('modalCtrl', function($scope,$http,testSrv ){
console.log(testSrv.getData());
});
});
})(); // EOF
The following is my angular application file
(function(){
// Angular App
var app = angular.module('adminApp', ['ngRoute', 'adminCtrls', 'siteSrvs']);
// Routing
app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){
// HTML5 Mode
$locationProvider.html5Mode({
enabled: true
});
}]);
})();
This is my service.
(function(){
// Phoenix Services
var phx = angular.module('siteSrvs',[]);
// Main
phx.service('testSrv', function($http){
// Data Storage
var data = '';
var answers = '';
// Returned Functions
return {
getData : _getData,
setData : _setData,
getAnswers : _getAnswers
}
// Get Data
function _getData(){
return data;
}
// Set Data
function _setData(t){
if(t) data = t;
}
function _getAnswers(){
return $http.get('/rest/test2/test2.php');
}
});
})(); // EOF
I am trying to set data in my first controller and the get the same data from my second controller.
My second controller cannot see the data which was stored by first controller.
I want to know if I'm missing any module injection??