[英]Can't bind to 'ngForOf' since it isn't a known property of 'li' in ngFor Angular
I saw another question about the same problem, but I don't know why I get this error.我看到另一个关于同一问题的问题,但我不知道为什么会出现此错误。 The odd thing here is that I did another component that returns a array and works fine, now I've created this new one but it doesn't work.奇怪的是,我做了另一个组件,它返回一个数组并且工作正常,现在我创建了这个新组件,但它不起作用。
Can't bind to 'ngForOf' since it isn't a known property of 'li'无法绑定到“ngForOf”,因为它不是“li”的已知属性
this is my component SubscriptionCustumerComponent:这是我的组件订阅客户组件:
public subscriptions:Subscription[];
constructor(private subscriptionService:CustumerSubscriptionService){}
ngOnInit(): void {
this.subscriptionService.getsubscriptionbyID(this.userid)
.subscribe(res=>this.subscriptions=res,
error=> this.errorMessage);
}
the page:这页纸:
<ul class="nav">
<li *ngFor="let data of subscriptions" >
<div class="card">
<img src="{{data.image}}" style="width:100%;height:250px;">
<h1>{{data.name}}</h1>
</div>
</li>
</ul>
the SubscriptionModule:订阅模块:
import { CommonModule } from "@angular/common";
import { NgModule } from "@angular/core";
import { CustumerSubscriptionService } from "./service/subscription.service";
import { CustumerSubscriptionAppComponent } from "./subscription.app.component";
import { CustumerSubscriptioRoutingModule } from "./subscription.route";
@NgModule({
declarations:[
CustumerSubscriptionAppComponent
],
imports:[
CommonModule,
CustumerSubscriptioRoutingModule
],
providers:[
CustumerSubscriptionService
]
})
export class CustumerSubscriptionModule{}
and the main Module AppModule:和主模块 AppModule:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
NavegacaoModule,
NgbModule,
ToastrModule.forRoot(),
HttpClientModule
],
providers: [httpInterceptorProviders],
bootstrap: [AppComponent]
})
export class AppModule { }
double check what your service is returning.仔细检查您的服务返回的内容。 Most likely this:最有可能的是:
this.subscriptions=res
Is not an array.不是数组。 And the most likely explanation for this is that you are returning a single record based on this.userid, instead of an array.对此最可能的解释是您返回的是基于 this.userid 的单个记录,而不是数组。
try to console.log(this.subscriptions)
to see what it is.尝试console.log(this.subscriptions)
看看它是什么。 If it's not an array, you will get that error.如果它不是数组,您将收到该错误。
You tell your component is called SubscriptionCustumerComponent
, however it is not declared in the SubscriptionModule
.你告诉你的组件叫做SubscriptionCustumerComponent
,但是它没有在SubscriptionModule
中声明。 Instead, we have CustumerSubscriptionAppComponent
.相反,我们有CustumerSubscriptionAppComponent
。 This could be an issue because all other things (including browser module and common module) seem to be correct.这可能是一个问题,因为所有其他内容(包括浏览器模块和公共模块)似乎都是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.