繁体   English   中英

原语的JavaScript分配不相等

[英]JavaScript assignment of primitive is not equal

我正在使用ng-class(来自angularjs)将一个类分配给分页表中的活动页码。 当按下“跳过10页”按钮时,它会调用一个函数并最终在此处结束:

if (this.scope.currentPage <= this.scope.lastPage - this.scope.pageNumbersGroupSize) {
    this.scope.currentPage += this.scope.pageNumbersGroupSize;
} else {
    while (this.scope.currentPage < this.scope.lastPage) {
        this.scope.currentPage++;
    }
    //this.scope.currentPage = this.scope.lastPage;
}

原始if语句中的代码可以按预期工作,而else中的代码则无法正常工作。 最初,我只有在else语句中注释掉的行:

this.scope.currentPage = this.scope.lastPage;

但由于某种原因,它没有激活我的HTML中的类:

<a ng-class="{'selectedPage' : pageNumber === currentPage, 'grayText' :
pageNumber !== currentPage}" ng-repeat="pageNumber in pageNumbers"
style="margin-right: 4px; cursor: pointer;" ng-click="goToPage(pageNumber)">
{{pageNumber}}
</a>

在HTML中,当pageNumber === currentPage时,应使用selectedPage类。 它不在原始代码中。 然后,仅检查由于我使用的某些原因分配是否不同:

this.scope.currentPage = angular.copy(this.scope.lastPage);

这仍然没有用。 因此,我尝试了顶部代码中的while循环。 这确实有效。 有人可以解释为什么我的原始代码不起作用吗? 此外,有没有比将while循环放入其中更好的方法来解决此问题? 谢谢!

弄清楚了:这是因为我使用的是===而不是==,并且我为lastPage使用的变量从数据库存储过程中以字符串而不是数字的形式返回。 它正在将字符串与===的数字进行比较,并且它们不相等。 我只是对数据库返回的值使用了parseInt(),它是固定的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM