我用directive做了一個分頁,在controller聲明了一個$scope.page,當(dāng)點擊下一頁的時候在directive就對這個$scope.page+1,但是在controller中卻沒有改變。
但是在directive中打印,page的值是變了的。
以下是相關(guān)代碼。
directive中:
routerApp.directive("pagination",['$http','localStorageService',function($http,localStorageService) {
return{
scope:{
refresh : '&',
count:'=',
pagesize:'=',
page:'='
},
restrict:'EAC',
replace: true,
templateUrl:'../index/page.html',
controller:function($scope,$element,$attrs,localStorageService){
$scope.totalPage=1;//總頁數(shù)
//首頁
$scope.firstPage=function(){
$scope.page=1;
}
console.log($scope.page)
//下一頁
$scope.nextPage= function () {
var page=parseInt($scope.page)+1;
if($scope.totalPage>=page){
$scope.page=page;
console.log($scope)
}else{
//toaster.pop('error', "消息", "已是最后一頁");
alert("已是最后一頁!")
}
}
//上一頁
$scope.upPage= function () {
var page=$scope.page-1;
if(page>0){
$scope.page=page;
}else{
alert("已是第一頁");
}
}
$scope.endPage=function(){
$scope.page=$scope.totalPage;
}
$scope.getList=function(){
if(!$scope.totalPage){}else{
var size=10;
var min=parseInt($scope.page)-1;
var max=parseInt($scope.page)+9;
if($scope.totalPage>10){
if(max>$scope.totalPage){
min=parseInt($scope.totalPage)-9;
max=$scope.totalPage;
} else{
if(min==0){
var min=$scope.page;
var max=parseInt($scope.page)+10;
}
}
}else{
size=$scope.totalPage;
min=1;
}
$scope.arrayObj = new Array([size]);//創(chuàng)建一個數(shù)組并指定長度,注意不是上限,是長度
for (var i=0;i<size; i++)
{
$scope.arrayObj[i]={key:"dd",value:parseInt(i)+parseInt(min)};
}
}
}
$scope.$watch('page', function(newVal, oldVal) {
if($scope.totalPage>=newVal&&newVal>=1){
$scope.page=newVal;
$scope.refresh();
}else{
$scope.page=oldVal;
alert("超出最大可查詢值");
}
// console.log( $scope.page);
$scope.getList();
});
//單個按鈕跳轉(zhuǎn)
$scope.setPage=function(page){
$scope.page=page;
}
$scope.$watch('pagesize', function(newVal, oldVal) {
$scope.refresh();
});
$scope.$watch('count', function(newVal, oldVal) {
var z =$scope.count%$scope.pagesize;
if(z==0){
if(Math.floor($scope.count/$scope.pagesize)==0){
$scope.totalPage=1;
}else{
$scope.totalPage=Math.floor($scope.count/$scope.pagesize);
}
}else{
$scope.totalPage=Math.floor($scope.count/$scope.pagesize)+1;
}
$scope.getList();
});
}
};
}]);
page.html
<div class="fy">
<ul class="clearfix">
<li><button ng-click="firstPage()">首頁</button></li>
<li><button ng-click="upPage()">上一頁</button></li>
<li ng-repeat="i in arrayObj"><span ng-class="{'active':(page==i.value)}" ng-click="setPage(i.value)">{{i.value}}</span></li>
<li><button ng-click="nextPage()">下一頁</button></li>
<li><button ng-click="endPage()">尾頁</button></li>
<li>共{{totalPage}}頁{{count}}條</li>
<li>跳到第<input type="text" ng-model="page">頁</li>
</ul>
</div>
controller
routerApp.controller('checkController', ['$scope','$http','localStorageService', '$filter','$location',function($scope,$http,localStorageService, $filter,$location) {
$scope.page=1;
$scope.pagesize=3;
$scope.nashuilist = function () {
console.log($scope)
//這里點擊下一頁再去執(zhí)行一遍這個方法,理論上來說點擊下一頁后這里的$scope.page應(yīng)該+1 =2了,但是打印后還是顯示為 1
var s=angular.element("#startTime").val();
var e=angular.element("#endTime").val();
$http({
method:"GET",
url:"/api/dz/nsdz",
params:{
"startTime":s,
"endTime": e,
"nsrsbh":$scope.nsrsbh,
"page":$scope.page,
"pageSize":$scope.pagesize
},
dataType:"json"
}).success(function (resp) {
if (resp.success) {
$scope.nslist=resp.data.listData;
$scope.count=resp.data.totalRow;
} else {
console.log(resp.message)
}
}).error(function (list) {
//處理錯誤
console.log("錯誤")
});
}
$scope.nashuilist();
}]);
頁面中是這樣引用的。
<pagination count="count" page="page" pagesize="pagesize" refresh="nashuilist()"></pagination>
相同的代碼,只不過方法名字換了一下,在另一個controller里面就沒有問題。查閱了很多資料
剛接觸前端不久,希望大神能幫助一下。
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。