0

i have used Angularjs and i wanna call getcustomer function from one controller to another controller i have so many doing gooogling but i don't have an idea that how to call that i have write below code which i used

      var app = angular.module('Napp', []);
            app.controller('GetAlphabetical', function ($scope, $http) {

      function getCutomers() {
                $scope.loading = true;
                $http.get('@Url.Content("~/Home/GetPesrons")').then(function (response) {
                    //var _data = angular.fromJson(response);
                    $scope.loading = false;
                    $scope.Customer = response.data; // please check the request response if list id in data object 
                }, function (error) {
                    throw error;
                })
            }
    });



and second controller :

app.controller('MainCtrl', function ($scope, $http) {
getCutomers()
});
Nayeem Mansoori
  • 821
  • 1
  • 15
  • 41
  • Generally its called a bad practice. You should start thinking to use either `service` or `factory`. You will found many articles of doing that http://stackoverflow.com/questions/22511986/angularjs-sharing-data-between-factory-controller-across-modules will help you. – Vineet Aug 21 '15 at 05:10
  • ya i know may possible you are right but i'm new in angularjs – Nayeem Mansoori Aug 21 '15 at 05:11
  • use "factory or service" instead of module to write global methods – Rohan Pawar Aug 21 '15 at 05:37
  • check for $broadcast, $emit and $on http://jsfiddle.net/simpulton/XqDxG/ – Nitin Chaurasia Oct 18 '15 at 09:11

3 Answers3

1

Mate, you will have to follow the following steps to resolve your problem. Firstly you have you create a factory

   angular
    .module('Napp')
    .factory('CustomerFactory', ['$http', function ($http) {
      var _factory = {};

      _factory.getCustomers = function () {
        return $http.get('@Url.Content("~/Home/GetPesrons")');
      };

      return _factory;
    }]);

Then you can share data and functions between multiple controllers or services

GetAlphabetical Controller :

   angular
    .module('Napp')
    .controller('GetAlphabetical', ['$scope', 'CustomerFactory', function ($scope, CustomerFactory) {

      loadCustomers();

      function loadCustomers() {
        CustomerFactory.getCustomers().then(function (successResponse) {
          $scope.Customer = successResponse.data; // please check the request response if list id in data object 
        }, function (errorResponse) {
          throw error;
        })
      }

    }]);

MainCtrl Controller :

  angular
    .module('Napp')
    .controller('MainCtrl', ['$scope', 'CustomerFactory', function ($scope, CustomerFactory) {

      loadCustomers();

      function loadCustomers() {
        CustomerFactory.getCustomers().then(function (successResponse) {
          $scope.Customer = successResponse.data; // please check the request response if list id in data object 
        }, function (errorResponse) {
          throw error;
        })
      }

    }]);
Mohan Singh
  • 883
  • 8
  • 18
  • You may wish to load data in one controller and use the same data in different controller. if this is your challenge then you can save your data in factory and access in different controllers. – Mohan Singh Aug 21 '15 at 05:53
  • Yes you are saying absolutely right i will try you idea – Nayeem Mansoori Aug 21 '15 at 05:55
  • Mohan please see my problem http://stackoverflow.com/questions/32152583/call-function-from-one-controller-to-another-controller-using-factory-in-angular?noredirect=1#comment52193975_32152583 – Nayeem Mansoori Aug 22 '15 at 05:19
0

What you want to do is to somehow communicate between two controllers. This can be easily be achieved using $broadcast & $on.

Incase there is a parent child relation between your controllers, use the following.

function firstCtrl($scope){
    $scope.$broadcast('someEvent', [1,2,3]);
}

function secondCtrl($scope){
    $scope.$on('someEvent', function(event, mass) {console.log(mass)});
}

If there is no parent child relationship between your controller, then inject $rootScope and broadcast using that.

related question - https://stackoverflow.com/a/14502755/1182982

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
0

This can be easily done by defining it as a service and injecting it as a dependency.

var app = angular.module('myApp', []);

myApp.service('helloWorldFromService', function() {
    this.sayHello = function() {
        return "Hello, World!"
    };
});

app.controller('MainCtrl', function ($scope, $http, helloWorldFromService) {

app.controller('GetAlphabetical', function ($scope, $http, helloWorldFromService) {

Angular Service

Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79