【问题标题】:AngularFire2 Can't bind to 'ngForAs' since it isn't a known property of 'li'AngularFire2 无法绑定到“ngForAs”,因为它不是“li”的已知属性
【发布时间】:2017-08-05 00:12:29
【问题描述】:

我正在尝试使用 *ngFor 循环从 firebase 数据库中循环出一个简单的字符串列表。但是我在控制台中收到一条错误消息:

Unhandled Promise rejection: Template parse errors:
Can't bind to 'ngForAs' since it isn't a known property of 'li'. ("  <ul>
    <li [ERROR ->]*ngFor="let item as items | async">
        {{item.$value}}
    </li>
"): AppComponent@1:7
Property binding ngForAs not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("  <ul>
    [ERROR ->]<li *ngFor="let item as items | async">
        {{item.$value}}
    </li>
"): AppComponent@1:3 ; Zone: <root> ; Task: Promise.then ; Value: SyntaxError {__zone_symbol__error: Error: Template parse errors:
Can't bind to 'ngForAs' since it isn't a known property of 'li'. ("  <…, _nativeError: ZoneAwareError, __zone_symbol__currentTask: ZoneTask, __zone_symbol__stack: "Error: Template parse errors:↵Can't bind to 'ngFor…ttp://localhost:4200/polyfills.bundle.js:6423:35)", __zone_symbol__message: "Template parse errors:↵Can't bind to 'ngForAs' sin…         {{item.$value}}↵    </li>↵"): AppComponent@1:3"} Error: Template parse errors:
Can't bind to 'ngForAs' since it isn't a known property of 'li'. ("  <ul>
    <li [ERROR ->]*ngFor="let item as items | async">
        {{item.$value}}
    </li>
"): AppComponent@1:7
Property binding ngForAs not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("  <ul>
    [ERROR ->]<li *ngFor="let item as items | async">
        {{item.$value}}
    </li>
"): AppComponent@1:3
    at SyntaxError.ZoneAwareError (http://localhost:4200/polyfills.bundle.js:6869:33)
    at SyntaxError.BaseError [as constructor] (http://localhost:4200/vendor.bundle.js:64389:16)
    at new SyntaxError (http://localhost:4200/vendor.bundle.js:5723:16)
    at TemplateParser.parse (http://localhost:4200/vendor.bundle.js:17589:19)
    at JitCompiler._compileTemplate (http://localhost:4200/vendor.bundle.js:47585:68)
    at http://localhost:4200/vendor.bundle.js:47468:62
    at Set.forEach (native)
    at JitCompiler._compileComponents (http://localhost:4200/vendor.bundle.js:47468:19)
    at createResult (http://localhost:4200/vendor.bundle.js:47350:19)
    at ZoneDelegate.invoke (http://localhost:4200/polyfills.bundle.js:6211:26)
    at Zone.run (http://localhost:4200/polyfills.bundle.js:6003:43)
    at http://localhost:4200/polyfills.bundle.js:6590:57
    at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.bundle.js:6244:31)
    at Zone.runTask (http://localhost:4200/polyfills.bundle.js:6043:47)
    at drainMicroTaskQueue (http://localhost:4200/polyfills.bundle.js:6423:35)

这是我用来循环的代码

<ul>
    <li *ngFor="let item as items | async">
        {{item.$value}}
    </li>
  </ul>

这是组件代码:

import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AngularFire, FirebaseListObservable } from 'angularfire2';

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

export class AppComponent {
  title = 'app works!';

  items: FirebaseListObservable<any[]>;

  constructor(private af: AngularFire){
    this.items = af.database.list('/items');
  }
}

我之前在其他项目上使用过 ngFor 循环加载,但我不明白为什么这不起作用,请帮助我 :)

谢谢

【问题讨论】:

    标签: angular typescript firebase angularfire2 ngfor


    【解决方案1】:
    <li *ngFor="let item as items | async">
    

    应该是

    <li *ngFor="let item of items | async">
                         ^^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 2022-01-09
      • 2018-06-09
      • 2017-06-21
      • 2017-02-03
      • 1970-01-01
      相关资源
      最近更新 更多