I have service methods,
var selectedType = 0;
.........
.........
return {
updateType: function (type) {
return selectedType = type;
},
getType: function () {
return selectedType;
}
}
I want to share this selectedType variable across 2 controllers. So calling the updateType method from one controller and opening a new popup page where I am calling the getType method.
Issue is, the getType method always returning 0 in popup page, but the value assigned from main page is 2 (by calling updateType method).
Main page,
angular.module('Controller1Module', [])
.controller('myController1', ['$scope', 'myService',
function ($scope, myService) {
myService.updateType(1);
$scope.$watch(myService.getType , function(newValue, oldValue){
$scope.selectedType = myService.getType();
});
}
Popup Controller,
angular.module('Controller2Module', [])
.controller('myController', ['$scope', 'myService',
function ($scope, myService) {
$scope.$watch(myService.getType , function(newValue, oldValue){
$scope.selectedType = myService.getType();
});
}
My Service,
angular.module('serviceModule', [])
.service('myService', ['$rootScope',
function($rootScope){
var selectedType = 0;
return {
updateType: function (type) {
return selectedType = type;
},
getType: function () {
return selectedType;
}
]);