我希望在angularjs中调用另一个函数内的函数.例如.我有一个从数据库中获取记录的功能,现在每次调用任何函数时我都需要从数据库中获取.
控制器: –
function SearchCtrl($scope, $http, $element) {
// i wish to put this into a function and call it in every function like add,search,etc.
$http.get('PHP/products.PHP').success(function(data){
$scope.products = data;
});
$scope.search = function() {
var elem = angular.element($element);
var dt = $(elem).serialize();
dt = dt+"&action=index";
//alert(dt);
console.log($(elem).serialize());
$http({
method: 'POST',
url: 'PHP/products.PHP',
data: dt,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status) {
console.log(data);
$scope.products = data; // Show result from server in our <pre></pre> element
}).error(function(data, status) {
$scope.data = data || "Request Failed";
$scope.status = status;
});
};
//~ add
$scope.add = function() {
var elem = angular.element($element);
var dt = $(elem).serialize();
dt = dt+"&action=add";
//alert(dt);
console.log($(elem).serialize());
$http({
method: 'POST',
url: 'PHP/products.PHP',
data: dt,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status) {
$scope.search(); //i wish to call the function like this instead of replicating the code as below each time
//~ $http.get('PHP/products.PHP').success(function(data){
//~ $scope.products = data;
}); // Show result from server in our <pre></pre> element
}).error(function(data, status) {
$scope.data = data || "Request Failed";
$scope.status = status;
});
};
我该怎么做呢?
解决方法:
如果我正确地阅读您的问题,您可以输入以下代码:
// i wish to put this into a function and call it in every function like add,search,etc.
$http.get('PHP/products.PHP').success(function (data) {
$scope.products = data;
});
在你的控制器中进入这样的函数:
var getProducts = function () {
$http.get('PHP/products.PHP').success(function (data) {
$scope.products = data;
});
};
的getProducts();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。