我有一个带有以下代码的Angular控制器”
var getFilms = function(searchterm, category, page){
dataService.getFilms(searchterm, category, page).then(response => {
let films = [];
let pageLinks = [];
console.log($routeParams.page += 1);
console.log($routeParams.page - 1);
for (let i = 0; i < response[10][0]; i += 10) {
let pageNum = i/10;
if (pageNum === $routeParams.page) {
pageLinks.push({
pageLink: "/wai-assignment/#/films?searchterm=" + searchterm + "&category=" + category + "&page=" + i/10,
pageNum: i/10
})
}
if (pageNum === ($routeParams.page - 1)) {
pageLinks.push({
pageLink: "/wai-assignment/#/films?searchterm=" + searchterm + "&category=" + category + "&page=" + i/10,
pageNum: i/10
})
}
if (pageNum === ($routeParams.page + 1)) {
pageLinks.push({
pageLink: "/wai-assignment/#/films?searchterm=" + searchterm + "&category=" + category + "&page=" + i/10,
pageNum: i/10
})
}
}
for (let i = 0; i < (response.length - 1); i++) {
films.push({
title: response[i].title,
category: response[i].name,
description: response[i].description
})
}
$scope.pageLinks = pageLinks;
$scope.films = films;
},
function(err){
$scope.status = 'Unable to load data' + err;
},
function(notify){
console.log(notify);
});
}
首先,在显示console.log($routeParams.page += 1)
的地方,输出为'01'而不是'1',这是为什么呢? $ routeParams.page是一个数字(我用typeof检查过)。
第二,此代码触发两次。这两个console.logs在控制台中出现两次。
第三,它们在第二次执行时输出的变量与第一次执行时不同:
controllers.js:26 01
controllers.js:27 0
controllers.js:26 011
controllers.js:27 10