Angularjs 指令加载时无法调用函数

Angularjs 指令加载时无法调用函数,angularjs,angularjs-directive,isolate-scope,Angularjs,Angularjs Directive,Isolate Scope,我正在研究AngularJS自定义指令。我正在尝试在指令加载后立即调用函数 我尝试在link函数中调用该函数,但它以TypeError的形式抛出错误:scope.setCurrent不是函数 这就是我想做的 function pagination() { return { restrict: 'EA', templateUrl: 'template/directives/pagination.html', scope: {

我正在研究AngularJS自定义指令。我正在尝试在指令加载后立即调用函数

我尝试在link函数中调用该函数,但它以TypeError的形式抛出错误:scope.setCurrent不是函数

这就是我想做的

function pagination() {
    return {
        restrict: 'EA',
        templateUrl: 'template/directives/pagination.html',
        scope: {
            totalData: '@',
        },
        link: dirPaginationControlsLinkFn
    };
}


function dirPaginationControlsLinkFn(scope, element, attrs){
    var vm = this;
    scope.setCurrent(1);    //this function is throwing error

    scope.setCurrent = function(num) {     //working fine
        scope.GetPager(scope.totalData,num,3);
    };
}
触发单击事件时,相同的setCurrentdata函数工作正常

{{pages}
我不确定我错在哪里。任何帮助都将不胜感激

问题在于您在声明之前调用函数,这就是为什么未定义setCurrent的原因

    scope.setCurrent = function(num) {     //first declare function and initialling with body 
        scope.GetPager(scope.totalData,num,3);
    };

    scope.setCurrent(1);    //than call function 

问题是在声明之前调用函数,这就是为什么并没有定义setCurrent

    scope.setCurrent = function(num) {     //first declare function and initialling with body 
        scope.GetPager(scope.totalData,num,3);
    };

    scope.setCurrent(1);    //than call function