angular - 加载更多按钮角度
问题描述
我已经使用 Angular 和 Typescript 实现了一个加载更多按钮我想在开始时查看 6 个帖子,然后一旦我点击它,就会显示另外 6 个帖子,该按钮正在工作,但是,除非我点击按钮,否则所有帖子都被隐藏。我附上html代码和打字稿。请指教。谢谢
HTML
`<button mat-button *ngIf="isMore" color="accent" class="load-more-container" (click)="onMore()">Load
more</button>`
打字稿
import {
Component,
OnInit,
ChangeDetectionStrategy,
Input,
} from "@angular/core";
import { Observable } from "rxjs";
import { BlogDetails, BlogResponse } from "../models/blog";
import { BlogService } from "../services/blog.service";
import { Router } from "@angular/router";
import { AuthService } from "../../../../providers/auth.service";
import { AuthenticationService } from "../../../../services/authentication.service";
import { SignupComponent } from "../../../../pages/authentication/signup/signup.component";
@Component({
selector: "anms-blog-list",
templateUrl: "./blog-list.component.html",
styleUrls: ["./blog-list.component.scss"],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BlogListComponent implements OnInit {
@Input() agentFirstName: string;
@Input() agentLastName: string;
@Input() agendId: string;
@Input() agentType: string;
@Input() agentEmail: string;
@Input() firstName: string;
@Input() lastName: string;
blogs: BlogDetails[];
blogArray$: Observable<BlogDetails[]>;
blogOutput$: Observable<BlogDetails[]>;
// paginatorEnabled: boolean;
moreData: any;
limit = 1;
isMore = true;
isLimit: number;
blogData: BlogDetails[];
constructor(
private blogService: BlogService,
private router: Router,
private authService: AuthService,
private authenticationService: AuthenticationService
) {}
ngOnInit(): void {
// this.paginatorEnabled = true;
this.blogArray$ = this.blogService.getBlogs();
// this.blogOutput$ = this.blogService.firstPaginate();
// this.blogOutput$.subscribe(blog => {
// this.blogs = blog as BlogDetails[];
// });
this.getData();
}
getData(){
this.blogService.getBlogs().subscribe(res=>{
this.blogData=res;
this.moreData = this.blogData.slice(0, this.limit * 6);
console.log(this.blogData)
console.log(this.moreData)
})
}
navigateBlog(id: string) {
this.router.navigateByUrl("/blogs/" + id);
}
editBlog(id: string) {
this.router.navigateByUrl("/blogs/edit/" + id);
}
onChangePage() {
// this.blogOutput$ = this.blogService.paginateNext();
// this.questionsOutput$.subscribe((question) => {
// this.questions = question as Question[];
// });
//if (this.questions.length === this.previousLength)
// this.paginatorEnabled = false;
//this.previousLength = this.questions.length;
// this.moreData = this.questions.slice(0, this.limit * 6);
}
onMore() {
this.isLimit = this.blogData.length - 6 * this.limit;
this.limit += 1;
this.getData();
if (this.isLimit > 6) {
this.isMore = true;
} else {
this.isMore = false;
}
}
createBlog() {
// if (this.isUserLoggedIn) {
// this.router.navigateByUrl('/blogs/add');
// } else {
// this.authenticationService.showSignUp(SignupComponent, {
// isAgent: null,
// redirect: '/blogs/add',
// userAutoLoginAfterSignUp: true
// });
// }
this.router.navigateByUrl("/blogs/add");
}
get agentName() {
return this.agentFirstName + " " + this.agentLastName;
}
get isUserLoggedIn() {
return this.authService.isCurrentSession;
}
}
解决方案
请在您迭代 blogData 的位置添加您的 html 代码。
根据您提供的内容,您从 ngOnit 调用了两次 getBlogs 服务,有什么特别的原因吗?
在您的 html 代码中,您是否使用异步管道订阅 this.blogArray$?您是否还在 html 模板中使用 this.blogData 和 this.moreData ?
推荐阅读
- python - 匿名函数的pandas .apply意外返回一系列函数对象
- python - 选择一个列表中的项目并将其链接到另一个列表中的项目
- vue.js - 使用 vanilla JS 生成 Vue 组件
- python - 替换 DataFrame 中的值的问题
- neo4j - Neo4j - 检查具有给定标签的节点是否存在的最有效方法
- c++ - 该代码中的错误是什么问题说要找到否。的元素少于当前元素?
- javascript - 如何将鼠标悬停在容器上以显示带有 Javascript 的阅读更多按钮?
- javascript - 从 spotify 请求访问代码时收到“格式错误或非法请求”
- java - 一旦用户按 2 次 y 和下一次 n,当输入 n 时如何添加 item1 和 item2 并显示总价
- wpf - 将数据库中的文本数据显示到 WPF RichTextBox 中