calling method from one controller to another controller in angular js

Praveen M P

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
            } 
        })
    };



});
k.makarov

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');
                    } 
                })
            };
    }]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular Calling Modal Open function from one controller to another

From Dev

Calling a method of another controller

From Dev

Calling a method from another controller in rails

From Dev

Calling a method through from another controller JavaFx

From Dev

Laravel: calling controller method from another location

From Dev

calling different Action Method from another Controller

From Dev

Calling another method from Modelclass to Controller

From Dev

AngularJs calling Controller from another one in directive

From Dev

Call a method from one controller inside another

From Dev

How to invoke method of a controller from another one?

From Dev

ocLazyLoad - one controller calling another

From Dev

call angular js 1 one controller in another controller notwroking

From Dev

Session is null when calling method from one controller to another...MVC

From Dev

Sails.js calling one controller action from another and passing additional params in req.body

From Dev

Is it a bad idea calling a controller from another controller?

From Dev

Calling non-action method from another controller

From Dev

Calling post method from another controller's view

From Dev

undefined method `each' for nil:NilClass when calling from another controller

From Dev

Angular js Controller method from HTML with a parameter

From Dev

Laravel - Running method of another controller in one controller

From Dev

calling a directive method from controller

From Dev

angular calling controller function in from directive controller

From Dev

Value set from one controller into service to be used on another controller returns no value on page refresh using angular js(new to angular)

From Dev

angular js call a method for a controller inside another controllers method

From Dev

Common method for calling a node function from Angular Controller

From Dev

Ember.js Calling model's method from controller

From Dev

Passing value from one controller to another controller

From Dev

Keep UIView from one controller to another controller

From Dev

Calling method in another view controller from modal view controller using a delegate

Related Related

  1. 1

    Angular Calling Modal Open function from one controller to another

  2. 2

    Calling a method of another controller

  3. 3

    Calling a method from another controller in rails

  4. 4

    Calling a method through from another controller JavaFx

  5. 5

    Laravel: calling controller method from another location

  6. 6

    calling different Action Method from another Controller

  7. 7

    Calling another method from Modelclass to Controller

  8. 8

    AngularJs calling Controller from another one in directive

  9. 9

    Call a method from one controller inside another

  10. 10

    How to invoke method of a controller from another one?

  11. 11

    ocLazyLoad - one controller calling another

  12. 12

    call angular js 1 one controller in another controller notwroking

  13. 13

    Session is null when calling method from one controller to another...MVC

  14. 14

    Sails.js calling one controller action from another and passing additional params in req.body

  15. 15

    Is it a bad idea calling a controller from another controller?

  16. 16

    Calling non-action method from another controller

  17. 17

    Calling post method from another controller's view

  18. 18

    undefined method `each' for nil:NilClass when calling from another controller

  19. 19

    Angular js Controller method from HTML with a parameter

  20. 20

    Laravel - Running method of another controller in one controller

  21. 21

    calling a directive method from controller

  22. 22

    angular calling controller function in from directive controller

  23. 23

    Value set from one controller into service to be used on another controller returns no value on page refresh using angular js(new to angular)

  24. 24

    angular js call a method for a controller inside another controllers method

  25. 25

    Common method for calling a node function from Angular Controller

  26. 26

    Ember.js Calling model's method from controller

  27. 27

    Passing value from one controller to another controller

  28. 28

    Keep UIView from one controller to another controller

  29. 29

    Calling method in another view controller from modal view controller using a delegate

HotTag

Archive