切片 Django API 结果时,角度切片 ( | slice:0:10 ) 不起作用

发布于 2025-01-11 15:57:57 字数 1480 浏览 4 评论 0原文

我试图将 Django API 响应与 50 个结果进行切片,只显示 10 个结果,并且下面的代码似乎不起作用。错误:

(variable) post: unknown
Object is of type 'unknown'.

如果没有管道,它会显示 50 个结果。

<div class="news-row news-row-link" *ngFor="let post of Posts['results'] | slice:0:10">
      <div class="news-cells">
        <a href="{{ post['link'] }}" class="news-cell nc-date">
          <span>
            <time datetime="" title="Sun Sep 26 2021">{{ post['pub_date'] | timeAgo }}</time> <!---->
          </span>
          <!---->
        </a>
        <!----> 
        <a href="{{ post['link'] }}" class="news-cell nc-title">
          <span class="title-text">
            <!---->
            <span>{{ post['title'] }}</span>
          </span>
          <!---->
        </a>
        <!----> <!----> <!---->
      </div>
  </div>

我的 component.ts

  Posts:any = [];

  ngOnInit(): void {
    this.getAllPosts();
  }

  getAllPosts(){
    this.service.getAllPosts().subscribe(data=>{
      this.Posts = data;
    })
  }

我的 API 服务

export class APIService {

  readonly BaseAPI = 'http://127.0.0.1:8000/api/v1/'

  constructor( private http:HttpClient ) { }

  getAllPosts():Observable<any[]>{
    return this.http.get<any[]>(this.BaseAPI + 'posts/');
  }
}

I'm trying to slice a Django API response with 50 results to only show 10 results and the code below doesn't seem to work. The error:

(variable) post: unknown
Object is of type 'unknown'.

Without the pipe, it displays 50 results.

<div class="news-row news-row-link" *ngFor="let post of Posts['results'] | slice:0:10">
      <div class="news-cells">
        <a href="{{ post['link'] }}" class="news-cell nc-date">
          <span>
            <time datetime="" title="Sun Sep 26 2021">{{ post['pub_date'] | timeAgo }}</time> <!---->
          </span>
          <!---->
        </a>
        <!----> 
        <a href="{{ post['link'] }}" class="news-cell nc-title">
          <span class="title-text">
            <!---->
            <span>{{ post['title'] }}</span>
          </span>
          <!---->
        </a>
        <!----> <!----> <!---->
      </div>
  </div>

My component.ts

  Posts:any = [];

  ngOnInit(): void {
    this.getAllPosts();
  }

  getAllPosts(){
    this.service.getAllPosts().subscribe(data=>{
      this.Posts = data;
    })
  }

My API Service

export class APIService {

  readonly BaseAPI = 'http://127.0.0.1:8000/api/v1/'

  constructor( private http:HttpClient ) { }

  getAllPosts():Observable<any[]>{
    return this.http.get<any[]>(this.BaseAPI + 'posts/');
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人迟暮 2025-01-18 15:57:57

您没有使用异步管道来显示 Post 的最新更新版本,因此这就是 html 只看到第一个空值的原因;

帖子:任意=[]; (为空)

使用BehaviorSubjects订阅新值,然后在html中使用async来更新html。您可以在此处了解 [AsyncPipe](https://angular.io/api/common/AsyncPipe.

将代码更改为

Post:BehaviorSubject= new BehaviorSubject(null);

然后当您从服务获取值时,只需推送到 Post

this.Post.next(data);

然后在 html 中,您可以使用异步管道显示最新发出的值

*ngFor="let post of (Posts['results'] | async ) | 切片:0:10"

干杯。

You are not using async pipe to show the lates updated version of Post so that's why the html only sees the first value which is empty;

Post: any = []; (is empty)

Use BehaviorSubjects to subscribe the new value and then use async in html to update the html. You can learn about [AsyncPipe here](https://angular.io/api/common/AsyncPipe.

Change your code to this

Post: BehaviorSubject<any[]> = new BehaviorSubject(null);

then when you get values from your service just push to Post

this.Post.next(data);

and then in your html you can show the latest emitted value using async pipe

*ngFor="let post of (Posts['results'] | async) | slice:0:10"

cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文