【问题标题】:myfunction() function call from one controller to another in angularjsangularjs中从一个控制器到另一个控制器的myfunction()函数调用
【发布时间】:2015-08-21 04:59:20
【问题描述】:

我使用过 Angularjs,我想从一个控制器调用 getcustomer 函数到另一个控制器我有很多人在谷歌搜索,但我不知道如何调用它 我写了下面我使用的代码

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

【问题讨论】:

  • 通常称为不好的做法。您应该开始考虑使用servicefactory。你会发现很多这样的文章stackoverflow.com/questions/22511986/…会帮助你。
  • 是的,我知道你可能是对的,但我是 angularjs 的新手
  • 使用“工厂或服务”而不是模块来编写全局方法
  • 检查 $broadcast、$emit 和 $on jsfiddle.net/simpulton/XqDxG

标签: javascript jquery asp.net-mvc angularjs


【解决方案1】:

朋友,您必须按照以下步骤来解决您的问题。首先你要创建一个工厂

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

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

      return _factory;
    }]);

然后你可以在多个控制器或服务之间共享数据和功能

GetAlphabetical 控制器:

   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 控制器:

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

    }]);

【讨论】:

  • 您可能希望在一个控制器中加载数据并在不同控制器中使用相同的数据。如果这是您的挑战,那么您可以将数据保存在工厂并在不同的控制器中访问。
  • 是的,你说的完全正确,我会试试你的想法
【解决方案2】:

这可以通过将其定义为service 并将其作为依赖项注入来轻松完成。

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

【讨论】:

    【解决方案3】:

    您想要做的是在两个控制器之间以某种方式进行通信。这可以使用$broadcast & $on 轻松实现。

    如果您的控制器之间存在父子关系,请使用以下内容。

    function firstCtrl($scope){
        $scope.$broadcast('someEvent', [1,2,3]);
    }
    
    function secondCtrl($scope){
        $scope.$on('someEvent', function(event, mass) {console.log(mass)});
    }
    

    如果您的控制器之间没有父子关系,则注入 $rootScope 并使用它进行广播。

    相关问题 - https://stackoverflow.com/a/14502755/1182982

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      相关资源
      最近更新 更多