我試圖訪問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>
更新:不知從$ctrl.tableMeta
它,因爲指令沒有得到更新值是從API來/異步
解決方案!:哦,我發現我錯了,我需要使用$watch
否則它得到舊值默認情況下是不確定的,因爲它不是通過API設置異步尚未
scope.$watch('currentPage',() => {
scope.start = (scope.currentPage - 1) * scope.perPage + 1
scope.end = Math.min(scope.currentPage * scope.perPage, scope.totalCount)
})
提供你的HTML時,你用你的指令。 – Curiousdev
'currentPage'需要從HTML傳遞! –
你可以分享你的tablePagination.html嗎? –