从指令到控制器的角度传递函数参数

时间:2014-11-25 07:20:20

标签: javascript angularjs angularjs-directive

我试图实现指令分页,我想将当前页面的编号从指令传递给控制器​​,然后使用此参数从控制器运行一个函数,但我得到了未定义。

<account-pagination pagination-config="paginationConfig" on-change="pageChanged()">
</account-pagination>

控制器:

$scope.pageChanged = function(page) {
    console.log(page); // undefined 
}

指令:

scope: {
      paginationConfig: '=',
      onChange: '&',
    }


$scope.moveToPage = function(numPage) {
    $scope.currentPage = numPage;
    getPaginData(numPage);
 }

function getPaginData(numPage) {
    $scope.onChange({page: numPage});
}

模板指令:

<li ng-repeat="num in numPages"><a ng-click="moveToPage(num)" ng-class="{pageActive: isActive(num)}">{{num}}</a></li>

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案:

<account-pagination pagination-config="paginationConfig" on-change="pageChanged">
</account-pagination>

scope: {
      paginationConfig: '=',
      onChange: '=',
    }

$scope.pageChanged = function(page) {
    console.log(page);
}

$scope.moveToPage = function(numPage) {
    $scope.currentPage = numPage;
    $scope.onChange(numPage);
 }