I am unable to render data in my template since property: product={} is returning empty object.
ProductComponent.ts--->
{
category$;
product={};
id;
constructor(
private router:Router,
private route:ActivatedRoute,
private categoryService:CategoryService,
private productService:ProductService)
{
this.category$= categoryService.getCategories();
this.id=this.route.snapshot.paramMap.get('id');
if(this.id)
this.productService.getProduct(this.id).take(1)
.subscribe(p=>this.product=p);
console.log(this.product)
}
this.product
is showing null. Although when I log "p" inside subscription,
is returning empty object. But just not outside the subscription.
I am not able to get product data on template also because of this empty
object.
TEMPLATE--->
<label for="title">Title</label>
<input #title="ngModel" [(ngModel)]="product.title" name="title"
id="title" type="text" class="form-control" required>
<div class="alert alert-danger" *ngIf="title.touched && title.invalid">
Title is required
</div>
HELP!!
Below is my ProductService
import { AngularFireDatabase } from 'angularfire2/database';
import { Injectable } from '@angular/core';
@Injectable()
export class ProductService {
constructor(private db: AngularFireDatabase) { }
create(product){
return this.db.list('/products').push(product);
}
update(productId, product)
{
return this.db.object('/products/' + productId).update(product);
}
deleteProduct(productId){
this.db.object('/products/' + productId).remove();
}
getAll(){
return this.db.list('/products');
}
getProduct(id){
return this.db.list('/products/' + id);
}
}