繁体   English   中英

myfunction()函数在angularjs中从一个控制器调用到另一个

[英]myfunction() function call from one controller to another in angularjs

我已经使用过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()
});

配合,您必须按照以下步骤解决问题。 首先,您要创建一个工厂

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

    }]);

您要做的是以某种方式在两个控制器之间进行通信。 使用$ 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

通过将其定义为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) {

角度服务

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM