2017-07-01 96 views
1

NgModel沒有在這個組件上工作。我已經導入了FormsModule和東西。其他功能(如* ngIf和角度* ngFor)在其他組件上運行良好。檢查出app.module。Angular 4 NgModel沒有綁定數據

幫助!

我爲此使用了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>

+0

您是否在瀏覽器控制檯中看到錯誤? – Aravind

+0

@Aravind不,沒有顯示任何東西 –

+0

@Aravind我試過這個沒有成功,但再次嘗試,現在它的工作原理!是一個簡單的錯誤。我是新來的角。非常感謝。 –

回答

0

rating是型你將如何使用它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>