3

I'm just getting key not the value from AngularFire2. I also tried the Peter Pham Question but the issue remain same.

"@angular/core": "^6.0.0",
"angularfire2": "^5.0.0-rc.11",

Firebase database:

enter image description here

category.service.ts:

import { map } from 'rxjs/operators';
import { AngularFireDatabase } from 'angularfire2/database';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class CategoryService {

  constructor(private db: AngularFireDatabase) { }

  getCategories(){

    return this.db.list('/categories').snapshotChanges().pipe(map(categories=>{
      return categories.map(a =>{
        const value = a.payload.val();
        const key = a.payload.key;
        return {key, ...value};
      })
    }));
  }
}

product-form.component.ts:

import { CategoryService } from './../../category.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-product-form',
  templateUrl: './product-form.component.html',
  styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {

  categories$;

  constructor(public categoryService: CategoryService) {
    this.categories$ = categoryService.getCategories();
   }



  ngOnInit() {
  }

}

product-form.component.html:

<select ngModel name="category" id="category" class="form-control">
   <option value="">- Select -</option>
   <option *ngFor="let c of categories$ | async" [value]="c.key">
    {{c.value.name}} 
   </option>
 </select>

Where I'm making error? Thanks

3
  • 2
    shouldn't this {{c.value.name}} just be {{c.name}} ? Commented Aug 28, 2018 at 14:16
  • @Und3rTow if I use {{c.name}} then value="undefined" and name work fine. Commented Aug 28, 2018 at 19:24
  • 2
    ok, then move this.categories$ = ... into ngOnInit Commented Aug 28, 2018 at 19:56

1 Answer 1

3

As recommended by Und3rTow now it's working. I have tried the following code:

product-form.component.html:

<select ngModel name="category" id="category" class="form-control">
  <option value="">- Select -</option>
  <option *ngFor="let c of categories$ | async" [value]="c.key">{{c.name}}</option>
</select>

product-form.component.ts:

import { CategoryService } from './../../category.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-product-form',
  templateUrl: './product-form.component.html',
  styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {

  categories$;

  constructor(public categoryService: CategoryService) { }

  ngOnInit() {
    this.categories$ = this.categoryService.getCategories();
  }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.