如何在 angularjs 中调用控制器中的函数?

How to call function in controller in angularjs?

如果 url 中的参数 ID 可用,那么我想调用函数。


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

你的问题是你在声明函数之前调用它。

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