Uttam Panara
Uttam Panara

Reputation: 549

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

Upvotes: 3

Views: 9649

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

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

Upvotes: 3

Related Questions