【发布时间】: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()
});
【问题讨论】:
-
通常称为不好的做法。您应该开始考虑使用
service或factory。你会发现很多这样的文章stackoverflow.com/questions/22511986/…会帮助你。 -
是的,我知道你可能是对的,但我是 angularjs 的新手
-
使用“工厂或服务”而不是模块来编写全局方法
-
检查 $broadcast、$emit 和 $on jsfiddle.net/simpulton/XqDxG
标签: javascript jquery asp.net-mvc angularjs