【问题标题】:How to access the static variable in ngFor in angular2如何在angular2中访问ngFor中的静态变量
【发布时间】:2016-10-21 09:33:48
【问题描述】:

我有访问组件静态变量的循环。如果我使用“let person of people()”它不起作用。这里的persons方法返回多个人..得到“TypeError:self.context.persons不是函数”错误

static persons: Array<PersonDirective> = [];    
get persons(){
    return PersonInvolvedComponent.persons;
}

html在下面

    <person-directive *ngFor="let person of persons(); #idx = index"     (remove) = "removePerson(idx)">
    </person-directive>

【问题讨论】:

  • 我不明白为什么这不起作用。 #idx = index 虽然无效。应该是let idx = index
  • 也许问题出在let person of persons()persons 之后的大括号?
  • 是的,get persons() 不能使用 persons() 调用
  • 是的..在没有 () 的情况下工作

标签: angular


【解决方案1】:

将模板更改为

<person-directive *ngFor="let person of persons; let idx = index"     (remove) = "removePerson(idx)">
</person-directive>

Getter 作为属性而不是方法被访问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2010-10-19
    • 2017-01-13
    • 2010-10-16
    • 1970-01-01
    相关资源
    最近更新 更多