Posts

Showing posts with the label angular5

How do I subscribe a global variable to a BehaviorSubject?

How do I subscribe a global variable to a BehaviorSubject? I'm building a login function and I want certain buttons in the app.component template to appear the moment the user has logged in. I am trying to use BehaviorSubject for this. I'm using an *ngIf for the buttons in the template and the condition is a variable called loggedIn . However when I try to initialize the loggedIn variable with the result of the SubjectBehavior subscription i get the error: *ngIf loggedIn ERROR in app.component.ts(10,3): error TS2322: Type 'Observable<boolean>' is not assignable to type 'boolean'. I don't know whats causing this because I am returning a boolean, not an observable. Here is the code of app.component: export class AppComponent { private loggedIn: boolean = this.loginService.isLoggedIn().pipe( map((isLoggedIn: boolean) => { // {3} if (!isLoggedIn){ return false; } return true; })); constructor(private loginS...

Angular not able to get the attribute contents in view

Angular not able to get the attribute contents in view The product.component.ts is a component from where I'm fetching the data from the backend using the httpclient Service but when I display the product variable in the ngOninit() I get undefined, so none of them are displayed in the view but when the value of product variable is logged using console.log() I get all the list of products from the backend am I doing anything wrong here? Below is the code. product.component.ts import { Component, OnInit } from '@angular/core'; import {Product} from '../app-models/product.model'; import {ProductService} from '../app-services/product.service'; @Component({ selector: 'app-product-listing', templateUrl: './product-listing.component.html' }) export class ProductListingComponent implements OnInit { product: Product; constructor(private productService: ProductService) { } ngOnInit() { this.productService.getAllProducts().subscribe( ...