我正在尝试在link
中访问我的作用域变量,但它们显示为未定义
/** @ngInject */
function tablePagination() {
return {
restrict: 'E',
template: require('./tablePagination.html'),
scope: {
currentPage: '=',
totalPages: '=',
totalCount: '=',
perPage: '='
},
link: function (scope) {
console.log(scope.currentPage, scope) // scope.currentPage is undefined
}
}
}
// controller for authlogin
module.exports = tablePagination
我尝试使用
@
而不是=
,并且更改了绑定以使用{{}}
,但仍未定义。我可以使用$observe
,但我想一次获取多个属性的值以进行一些计算。有什么最好的方法吗?
HTML代码
<table-pagination
current-page="$ctrl.tableMeta.currentPage"
total-count="$ctrl.tableMeta.totalCount"
total-pages="$ctrl.tableMeta.totalPages"
per-page="$ctrl.tableMeta.perPage"></table-pagination>
更新: 我想这可能是因为指令没有从 API/异步获取的$ctrl.tableMeta
中获取更新的值。
解决方案!: 哦,我发现我的错误在于我需要使用$watch
,否则它会得到旧值,而默认情况下未定义,因为它尚未由API异步设置。
scope.$watch('currentPage', () => {
scope.start = (scope.currentPage - 1) * scope.perPage + 1
scope.end = Math.min(scope.currentPage * scope.perPage, scope.totalCount)
})
currentPage
需要从 HTML 中传递! - Dhaval Marthak