0
지시어에서 컨트롤러에 정의 된 함수에 두 개의 인수를 전달해야합니다.각도 지시자에서 컨트롤러 함수에 하나 이상의 인수 전달
HTML
<div paginate="allResults" change-page="changePage()"></div>
지침
app.directive('paginate', function() {
return {
scope: {
allResults: '=paginate',
changePage:'&'
},
template: '<ul class="pagination" ng-show="totalPages > 1">' +
' <li><a ng-click="firstPage()">«</a></li>' +
' <li><a ng-click="prevPage()">‹</a></li>' +
' <li ng-repeat="n in pages">' +
' <a class="current-{{n==current_page}}" ng-bind="n" ng-click="setPage(n)">1</a>' +
' </li>' +
' <li><a ng-click="nextPage()">›</a></li>' +
' <li><a ng-click="last_page()">»</a></li>' +
'</ul>',
link: function (scope) {
scope.nextPage = function() {
if (scope.current_page < scope.totalPages) {
scope.current_page++;
}
};
scope.prevPage = function() {
if (scope.current_page > 1) {
scope.current_page--;
}
};
scope.firstPage = function() {
scope.current_page = 1;
};
scope.last_page = function() {
scope.current_page = scope.totalPages;
};
scope.setPage = function (page) {
scope.current_page = page;
};
var paginate = function (results, oldResults) {
if (oldResults === results) return;
scope.current_page = results.current_page;
scope.total = results.total;
scope.totalPages = results.last_page;
scope.pages = [];
for (var i = 1; i <= scope.totalPages; i++) {
scope.pages.push(i);
}
};
var pageChange = function (newPage, last_page) {
scope.changePage(newPage, last_page);
};
scope.$watch('allResults', paginate);
scope.$watch('current_page', pageChange);
}
}
});
컨트롤러
function CareerCtrl($scope, $http, Career) {
$scope.init = function() {
Career.get({}, function (response) {
$scope.careers = response.careers.data;
$scope.allResults = response.careers;
}, function (error) {
console.log(error);
$scope.careers = [];
});
}; // init
$scope.changePage = function(newPage, last_page) {
console.log(newPage);
console.log(last_page);
if (newPage == last_page) return;
Career.get({
page: newPage
}, function (response) {
angular.copy(response.careers.data,scope.allResults.data);
scope.allResults.current_page = response.careers.current_page;
}, function (error) {
console.log(error);
$scope.allResults.data = [];
});
}
} // Ctrl
이제 컨트롤러에 newPage
및 last_page
에 대해 undefined
이 표시됩니다.
감사합니다. 나는 이와 같은 자세한 설명을 좋아합니다. – devo