Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function in controller in angularjs?

If in url parameter id is available then i want to call function.


    .controller('repeatCtrl', function($scope,$state,$stateParams) {
        if($stateParams.id != ""){
            //Here i want to call itemDetails function 
            $scope.itemDetails(id);
        };
        $scope.itemDetails = function(id) {
            // function body here!!
            alert(id);
        };
    })

like image 905
Uttam Panara Avatar asked Mar 19 '26 13:03

Uttam Panara


1 Answers

You issue is you are calling a function before making declaration of it.

.controller('repeatCtrl', function($scope,$state,$stateParams) {
    $scope.itemDetails = function(id) {
        // function body here!!
        alert(id);
    };
    if($stateParams.id && $stateParams.id != ""){
        //Here i want to call itemDetails function 
        $scope.itemDetails(id);
    };
})
like image 172
Pankaj Parkar Avatar answered Mar 22 '26 04:03

Pankaj Parkar