I'm having two controllers namely AController and BController.
I need to get the value of Controller AController's $scope.myName to the Controller BController's $scope.myName.
Note: Here I'm not passing the value, I'm trying to fetch the values.
My Sample AngularJS Script
var app = angular.module("DemoProject", ['ngRoute', 'ngAnimate', 'ngMessages', 'ngMaterial']);
app.controller('AController', function ($scope, $location, $rootScope, $document, $window) {
$scope.myName = 'Bala';
});
app.controller('BController', function ($scope, $location, $rootScope, $document, $window) {
$scope.myName = AController.$scope.myName;
});
Broadcast and other functionalities are for passing the values. I think not for fetching the values. Kindly assist me.
Note: Don't pass the value form AController. I need a solution how to fetch. Kindly assist me.
Difference Between my Post How to get Values from Other AngularJs Controller $scope variable and Global variables in AngularJS
The Content of the Post is
I have a problem where i'm initialising a variable on the scope in a controller. Then it gets changed in another controller when a user logs in. This variable is used to control things such as the navigation bar and restricts access to parts of the site depending on the type of user, so its important that it holds its value. The problem with it is that the controller that initialises it, gets called again by angular some how and then resets the variable back to its initial value.
I assume this is not the correct way of declaring and initialising global variables, well its not really global, so my question is what is the correct way and is there any good examples around that work with the current version of angular?
Explanation : How my Post Differ from the said Post
Kindly see the sentence "Value changed in another controller when a user logs", it means assume the Controller B tried to pass a value via Broadcast to Controller A to set the $scope.myName
.
Here, my requirement is how to get the value or how to fetch the value of $scope.myName from AController within BController.
First understand the post, then mark it as Duplicate.