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

时间:2015-06-03 06:08:27

标签: angularjs angularjs-scope angularjs-controller angular-ui-router

如果在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);
        };
    })

1 个答案:

答案 0 :(得分:2)

问题是你在声明它之前正在调用一个函数。

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