1

I want to call a method from one controller to another controller. There are two controllers named "header" and "mainContent". Need to call a "trigger method" in the "header Controller", After the success call of "result method" in the mainController.
If that method called that should hide that paragraph.

<div ng-controller="header">
  <p ng-show="msg">Content</p>
</div>
<div ng-controller="mainContent">
</div>


var module = angular.module("sourceViewer", ['ui.router']);
//header controller
module.controller('header', function ($scope, $location) {
      $scope.msg=true;
       $scope.trigger= function(data) { //This method should be called after the result method called in the mainContent Controller
            $scope.$on('UPDATE_CHILD', function() {
             if(data)
             $scope.msg=false;
            });

       }

});

// mainContent controller
module.controller('mainContent', function ($scope, $location, dataService) {

    $scope.user = dataService.user;
    $scope.signIn = function (user) {

        var result = dataService.login(user);
        result.success(function (data) {
            if (data.message== "success") {
                $scope.$broadcast('UPDATE_CHILD');
             //From here I want to call trigger method of header controller
            } 
        })
    };



});
2
  • why u don`t want to use $emit/$broadcast with $rootScope? Commented Jan 20, 2015 at 10:57
  • @k.makarov I tried that one also but it didn't work for me. Commented Jan 20, 2015 at 10:59

3 Answers 3

3

did u try this?

module.controller('header', ['$scope', '$location', '$rootScope', function ($scope, $location, $rootScope) {
              $scope.msg=true;
               $scope.trigger= function(data) { 
                  if(data)
                  $scope.msg=false;
               };

               $rootScope.$on('event:fire', $scope.trigger);

        }]);

        // mainContent controller
        module.controller('mainContent', ['$scope', '$location', 'dataService', function ($scope, $location, dataService) {

            $scope.user = dataService.user;
            $scope.signIn = function (user) {

                var result = dataService.login(user);
                result.success(function (data) {
                    if (data.message== "success") {
                      $rootScope.$broadcast('event:fire');
                    } 
                })
            };
    }]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @k.makarov it worked.. there is a spelling mistake in the $rootScope.$on('evet:fire', $scope.trigger); can you please change that
0

You can use $rootScope like:

<div ng-controller="header">
    <p ng-show="$root.header.msg">Content</p>
</div>
<div ng-controller="mainContent">
</div>

var module = angular.module("sourceViewer", ['ui.router']);
//header controller
module.controller('header', function ($rootScope,$scope, $location) {
  $rootScope.header.msg = true;
});

// mainContent controller
module.controller('mainContent', function ($rootScope,$scope, $location, dataService) {

$scope.user = dataService.user;
$scope.signIn = function (user) {

    var result = dataService.login(user);
    result.success(function (data) {
        if (data.message== "success") {
         $rootScope.header.msg = true;
        } 
    })
};
});

2 Comments

Thanks @kwaky.....I tried your code but it's giving a console error like: TypeError: Cannot set property 'msg' of undefined
try to add in top of your controller 'mainContent' this: ` $rootScope.header.msg = false; `
0

in the follwoing code you can see headerController is calling alert in mainController

myApp = angular.module("myApp",[]);

myApp.service("myService", function(){
    showAlertBool = false;
    return {
        showAlert: function (value) {
               showAlertBool = value;
        },

        canShowAlert: function () {
            return showAlertBool;
        }
    }
});

myApp.controller("headerController", function($scope, myService){
    console.log(myService);
    $scope.clickHandler = function(){
        myService.showAlert(true);
    }
});

myApp.controller("mainController", function($scope, myService){
    console.log(myService);
    $scope.getServiceValue = function(){
        return myService.canShowAlert();
    }

    $scope.$watch("getServiceValue()", function(newValue, oldValue){
        if(newValue === true && newValue !== oldValue){
            myService.showAlert(false);
            alert("I can show Alert now!!!");
        }
    });
});

For a working code you can go here

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.