簡體   English   中英

Angular 4 NgModel不綁定數據

[英]Angular 4 NgModel not binding data

NgModel在此組件上不起作用。 我已經導入了FormsModule和其他東西。 * ngIf和angled * ngFor等其他功能在其他組件上也可以正常工作。 還要檢查app.module。

救命!

我為此使用firebase和angularfire2。

我的組件:

 import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Params } from '@angular/router'; import { CommonModule } from '@angular/common'; import { AngularFireAuth } from 'angularfire2/auth'; import { AngularFireDatabase, FirebaseObjectObservable } from 'angularfire2/database'; import * as firebase from 'firebase/app'; import { Rating } from '../rating'; import 'rxjs/add/operator/SwitchMap'; @Component({ selector: 'app-view-rating', templateUrl: './view-rating.component.html', styleUrls: ['./view-rating.component.css'] }) export class ViewRatingComponent implements OnInit { name: any; rating: FirebaseObjectObservable<any>; user: firebase.User; item: any; constructor( private route: ActivatedRoute, private afAut: AngularFireAuth, private afDb: AngularFireDatabase) { this.afAut.authState.subscribe((auth) => { this.user = auth; }) } ngOnInit(): void { this.name = this.route.snapshot.paramMap.get('item'); this.rating = this.afDb.object(this.user.uid + '/' + this.name); console.log(this.rating) } } 

和HTML:

 <div class="form-container"> <div *ngIf="rating" > <input [(ngModel)]="rating.name" name="name" > </div> </div> 

ratingobservable的類型,您將如何將其用於ngModel 您需要訂閱它並分配給其他對象,然后將該對象用作ngModel

聲明任何類型的對象,並且如下所示不可觀察,

ratingObject:any;

this.rating = this.afDb.object(this.user.uid + '/' + this.name);
this.rating.subscribe(data =>{
        this.ratingObject = data;
});

然后在下面的HTML中使用,

<div *ngIf="ratingObject" >
    <input [(ngModel)]="ratingObject.name" name="name" >
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM