[英]myfunction() function call from one controller to another in angularjs
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 我已经使用过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()
});
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 : 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 Controller : 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;
})
}
}]);
What you want to do is to somehow communicate between two controllers. 您要做的是以某种方式在两个控制器之间进行通信。 This can be easily be achieved using $broadcast & $on . 使用$ 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. 如果您的控制器之间没有父子关系,则注入$ rootScope并使用它广播。
related question - https://stackoverflow.com/a/14502755/1182982 相关问题-https://stackoverflow.com/a/14502755/1182982
This can be easily done by defining it as a service
and injecting it as a dependency. 通过将其定义为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.