3

i wish to call a function inside another function in angularjs. for eg. i have a function to fetch records from database and now i need to fetch from database every time any function gets called. controller:-

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

how do i do this?

1
  • because i am using php as my model and i thought if anyone has worked with php as model and faced the same issue then it would be of some help Commented Sep 19, 2012 at 10:36

1 Answer 1

6

If I'm reading your question properly you can put this code:

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

into a function like this in your controller:

var getProducts = function () {
    $http.get('php/products.php').success(function (data) {
        $scope.products = data;
    });
};

and call it wherever you want in that same controller:

getProducts();

Sign up to request clarification or add additional context in comments.

Comments

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.