0

I have already defined a function in angularjs controller. But if I call it from somewhere in the same controller it's not working.

controller.js

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

In the above code $scope.update(); is not working. I have searched so many things in google but they have called in the same way but in my case it's not working. I don't know where am i wrong.

2
  • 5
    do you mean $scope.update(); ? you call it before you declare it Commented Apr 19, 2016 at 5:24
  • yes, that was the problem. Thank you Commented Apr 19, 2016 at 5:39

1 Answer 1

1

As @svarog said you have the call to the function before the declaration of it, so try this.

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes it's okay. But it doesn't going to ` if($scope.selectedCode == 1){ vm.purchased = "yes";` and it directly goes to ` vm.purchased = "no";`. What should i do now?
i fixed the issue, Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.