我已经在angularjs控制器中定义了一个函数。但是,如果我从同一个控制器的某个地方调用它,它就无法正常工作。
function ManageProductController($http, $scope, $mdDialog, $document, $location, $localStorage)
{
var vm = this;
vm.uid = $localStorage._id;
vm.purchased = '';
$scope.Types = [{code:1, type:'Available Items'}, {code:2, type:'Purchased Items'}, {code:3, type:'Guest Contributed'}, {code:4, type:'Full List'}];
$scope.update();
$scope.update = function() {
if($scope.selectedCode == 1){
vm.purchased = "yes";
}else if($scope.selectedCode == 2){
vm.purchased = "";
}else{
vm.purchased = "no";
}
$http({
url: 'http://localhost:7200/api/manage-product',
method: 'POST',
data: {userId:vm.uid, code:vm.purchased}
}).success(function(res) {
//$scope.productlist = res;
//console.log(vm.result);
vm.result = res.result;
vm.count=vm.result.length;
//console.log(vm.result);
if(vm.count == 0){
vm.showMessage = true;
} else {
vm.result=res.result;
vm.showMessage = false;
}
//console.log(vm.result);
//vm.docs=res.docs;
}, function(error) {
console.log(error);
alert('here');
});
};
}
在上面的代码$scope.update();
中无效。我在谷歌搜索了很多东西,但他们以同样的方式打电话,但在我的情况下它不起作用。我不知道我哪里错了。
答案 0 :(得分:1)
正如@svarog所说,在声明函数之前你已经调用了函数,所以试试吧。
function ManageProductController($http, $scope, $mdDialog, $document, $location, $localStorage)
{
var vm = this;
vm.uid = $localStorage._id;
vm.purchased = '';
$scope.Types = [{code:1, type:'Available Items'}, {code:2, type:'Purchased Items'}, {code:3, type:'Guest Contributed'}, {code:4, type:'Full List'}];
$scope.update = function() {
if($scope.selectedCode == 1){
vm.purchased = "yes";
}else if($scope.selectedCode == 2){
vm.purchased = "";
}else{
vm.purchased = "no";
}
$http({
url: 'http://localhost:7200/api/manage-product',
method: 'POST',
data: {userId:vm.uid, code:vm.purchased}
}).success(function(res) {
//$scope.productlist = res;
//console.log(vm.result);
vm.result = res.result;
vm.count=vm.result.length;
//console.log(vm.result);
if(vm.count == 0){
vm.showMessage = true;
} else {
vm.result=res.result;
vm.showMessage = false;
}
//console.log(vm.result);
//vm.docs=res.docs;
}, function(error) {
console.log(error);
alert('here');
});
};
$scope.update();
}