简体   繁体   English

运行 ngFor 时组件内的类函数给出错误(不是函数)

[英]Class function inside a component while running ngFor gives Error (not a function)

I'm trying to show a list of topics where every list entry is a custom component.我试图显示一个主题列表,其中每个列表条目都是一个自定义组件。 I will describe my problem as a simple example of my code.我将我的问题描述为我的代码的一个简单示例。

Using the current (02/2020) Version of Angular, MongoDB and Chrome使用当前 (02/2020) 版本的 Angular、MongoDB 和 Chrome

Topic class:主题类:

export class Topic {
    constructor(
        public title: string,
        public solutionID: number[] = [],
        private rating: number = 0,
        private votes: number = 0
    ) { }

    currentRating(): number {
        return this.rating / this.votes;
    }

    vote(stars: number) {
        this.votes++;
        this.rating += stars;
    }


    lastEditDate(): Date {
        console.log('test');
        return this.ts_worker[this.ts_worker.length - 1];
    }
} 

main-view.component.html This is the "frame" where the list is shown main-view.component.html这是显示列表的“框架”

<div class="content-wrapper">
    <app-topic-view *ngFor="let tp of topics" [topic]="tp"></app-topic-view>
</div>

main-view.component.ts This is where my topics come from (GET from Server) main-view.component.ts这是我的主题的来源(从服务器获取)

import { Component, OnInit, Input } from '@angular/core';
import { TopicsService } from 'src/app/services/topics.service';
import { Topic } from 'src/app/classes/class_Topic';

@Component({
  selector: 'app-main-view',
  templateUrl: './main-view.component.html',
  styleUrls: ['./main-view.component.scss']
})

export class MainViewComponent implements OnInit {

  @Input() topics: Topic[];

  constructor(private topicService: TopicsService) { }

  ngOnInit() {
    this.topicService.getAllTopics().subscribe((topics: Topic[]) => {
      this.topics = topics;
    })
  }

}

topic-view.component.html主题视图.component.html

<div class="topicElement">
    <!-- Some code hidden here -->
    <div class="back-group">
        <div class="solutionCount">Solutions: {{(topic.ts_worker[topic.ts_worker.length - 1])}}</div>
        <div class="solutionCount">Solutions: {{(topic.lastEditDate())}}</div>
    </div>
</div>

The error is found in {{(topic.lastEditDate()}} .{{(topic.lastEditDate()}}发现了错误。

The line above that works just fine.上面那行工作得很好。 Only the function call doesn't work.只有函数调用不起作用。

Error错误错误“lastEditDate 不是函数”的图像

Goal目标它的外观呈现方式


What am I missing here我在这里错过了什么

In the end I would like to use the functions of my class.最后,我想使用我班级的功能。 I'm used to do this in other languages.我习惯用其他语言来做这件事。 Is this possible in Angular?这在 Angular 中可能吗?

EDIT: Typo fixed编辑:错别字已修复

your "topics" are not class Topic when return of the http because when it's traspile to javaScript, you only has an object, you need create as返回 http 时,您的“主题”不是类主题,因为当它传输到 javaScript 时,您只有一个对象,您需要创建为

 this.topicService.getAllTopics().subscribe((topics: Topic[]) => {
      this.topics=x.map(x=>new Topic(x.title,x.solutionID,x.rating,x.votes))
    })

Others idea is that Topic was其他人的想法是 Topic 是

export class Topic {
  public title;
  public solutionID;
  private rating;
  private votes;
  constructor({ title, solutionID, rating, votes }) {
    this.title = title;
    this.solutionID = solutionID;
    this.rating = rating;
    this.votes = votes;
  }
  ..your methods...
}

And write和写

this.topicService.getAllTopics().subscribe((topics: Topic[]) => {
      this.topics=x.map(x=>new Topic(x))
    })

And another idea are that in your app-topic-view you has另一个想法是,在您的 app-topic-view 中,您有

private _topic:Topic
@Input() set topic(value)
{
  this._topic=new Topic(value.title,value.solutionID,value.rating,value.votes)
  //or if you change the constructor
  // this._topic=new Topic(value);
}

get topic()
{
     return this._topic;
}

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

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