-1

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.

Community
  • 1
  • 1
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
  • 1
    It's recommended to share code or state across controllers using services - https://docs.angularjs.org/guide/services – Zack Macomber Feb 18 '16 at 12:49
  • 2
    Possible duplicate of [Global variables in AngularJS](http://stackoverflow.com/questions/11938380/global-variables-in-angularjs) – Zack Macomber Feb 18 '16 at 12:52
  • @ZackMacomber The said post talks about the value passing not on value fetching. There is no relevant post, after googled then I post this question. Kindly assist me... – B.Balamanigandan Feb 18 '16 at 13:00
  • You could use `$rootScope` but then you're working with global state which is discouraged by the Angular team. Again, the recommended way to share things between controllers (whether fetching or passing) is via services. – Zack Macomber Feb 18 '16 at 13:04
  • Is this a scope inheritance problem? If so, read [AngularJS Developer Guide -- Scope Heirarchies](https://docs.angularjs.org/guide/scope#scope-hierarchies) and [AngularJS Wiki -- Understanding Scopes](https://github.com/angular/angular.js/wiki/Understanding-Scopes). – georgeawg Feb 18 '16 at 13:21
  • @B.Balamanigandan check out my answer. I hope it helps. Sharing data across angular controllers should be done with a service. – Ashkan Hovold Feb 18 '16 at 13:41

1 Answers1

3

Use a service like this

 var app = angular.module("DemoProject", ['ngRoute', 'ngAnimate', 'ngMessages', 'ngMaterial']);

 app.controller('AController', function ($scope, $location, $rootScope, $document, $window, myService) {
    $scope.myName = myService.myName;
    myService.myName = "Bala";

});

 app.controller('BController', function ($scope, $location, $rootScope, $document, $window, myService) {
    $scope.myName = myService.myName;

});

 app.factory('myService', function() {
    return {
      myName: ''
    }
});
Ashkan Hovold
  • 898
  • 2
  • 13
  • 28