1

How to fetch the scope value from one controller to another controller html

<button class="btn btn-info" ng-click="setLanguage('en')">English</button>
<button class="btn btn-info" ng-click="setLanguage('de')">Danish</button>

Javascript

  .controller('demoCtrl', function($scope) {
    $scope.setLanguage = function(language) {
        $scope.language = language;    
       }
    });
3
  • to get the value you have to use service Commented Jan 28, 2016 at 5:43
  • I tried To use services but its no use.... Can you try and give me the link Commented Jan 28, 2016 at 5:49
  • stackoverflow.com/questions/12008908/… refer this link Commented Jan 28, 2016 at 6:03

3 Answers 3

1

You can use a service to get the value set by one controller into another controller.

.service('someService', function () {
  this.language = null;
})

Controller

.controller('demoCtrl', function($scope, $rootScope, someService) {
    $scope.setLanguage = function(language) {
        $scope.language = language;
        someService.language = language;
        $rootScope.$broadcast('languageChanged');
    } 
});

In the other controller

.controller('someCtrl', function($scope, $rootScope, someService) {
    $rootScope.$on('languageChanged', function () {
        $scope.language = someService.language;
    }
});
Sign up to request clarification or add additional context in comments.

10 Comments

Are you sure that the language is getting set in the demoCtrl. Could you console.log(someService.language) in the $scope.setLanguage method after setting someService.language?
Ya i tried in demo ctrl in that its getting value name... but while giving console to servive and in another controller its getting null value
Could you create a fiddle for this?
Actually we used seperate files for header, sidebar and so on. two buttons are on navbar called "En" and "De". if we click en then lang is en and if we click de then lang should be de. so for nav bar we used notification.js and templates we used marker.js and different controller
I tried but its getting error TypeError: $rootScope.broadcast is not a function
|
0

You can share data between controllers with the help of either a service or you can use $emit() and $broadcast() functions.

There is a listener function in AngularJS $on() which will always be listening to the broadcast and emit events.

The syntax for the above function :

$scope.$emit(‘name1’,{}); //here you can send your data
$scope.$broadcast(‘name2’,{});
$scope.$on(‘name2’,function(event,args){
});

When using a service you can simply inject the service into your two controllers and share the data.

.service('someService', function () {
   var self=this;
   self.language = '';
});

.controller('firstCtrl', function($scope, someService) {
    $scope.setLanguage = function(language) {
    $scope.language = language;
    someService.language = language;
    } 
});

.controller('secondCtrl', function($scope, someService) {
    $scope.language = someService.language;
 });

2 Comments

I tried but while console the service value its not getting any value.... Its giving blank value
can you create a plunkr or jsfiddle for the same
0

If we want to call example method of:
"test1 DIV controller"
from:
"test2 DIV controller"
then we can write above method.

angular.Element(document.getelementByID('testDIV')).scope().example method name();

in test2DIVcontroller.

For Example :

<DIV ID="test1DIV" ng-controller="test1DIV controller"></DIV>
<DIV ID="test2DIV" ng-controller="test2DIV controller"></DIV>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.