How to pass value to index.html from component in angular 2


Santosh Hegde

I have an angular2 project which index.htmlcontains a title bar. Other components will be responsible for logging in and displaying other content.

I have to display the logo in the title bar, index.htmlonly when the user is logged in. I set a flag app.component.tswhen the user logs in . How can I reference this flag in index.html?

<body>
    <div class="content-body">
        <header class="header">
            <div class="header-bar container">
                <div class="header-bar__main">
                    <div class="header-heading">
                        <h1 class="page-title">noHold Application</h1>
                    </div>
                </div>
                <a class="btn btn--small btn--icon ng-scope" title="Logout"><span class="icon-sign-out"></span></a> // this line should be displayed only if user logs in.
            </div>
        </header>
        <div class="content">
            <div class="content__main">
                <div class="container">
                    <app-root>Loading... </app-root>        
                </div>
            </div>
        </div>
    </div>

</body>

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  static loggedIn = false;

  getLoggedIn() {
    return AppComponent.loggedIn;
  }

}
Burak Tasci

In Angular, the best practice is to have a single bootstrap component ( AppComponentin many cases and in your case ), and then define other components ( such as header, menu, page content, login status, etc. ).

In this case, it is recommended that you modify app.component.htmland introduce child components with their own selectors. E.g:

app.component.html

<header></header>
<router-outlet></router-outlet>

The content HeaderComponentuses the rendered headertag/selector, while the content of the navigation widget (eg: AboutComponent) uses the rendered router-outlettag/selector.

header.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css']
})
export class HeaderComponent {
  public loggedIn = false;

  ...
}

header.component.html

  <header class="header">
    <div class="header-bar container">
      <div class="header-bar__main">
        <div class="header-heading">
          <h1 class="page-title">noHold Application</h1>
        </div>
      </div>
      <div *ngIf="loggedIn">
        <a class="btn btn--small btn--icon ng-scope" title="Logout"><span class="icon-sign-out"></span></a>
      </div>
    </div>
  </header>

Hope this helps you.

Related


How to pass value to index.html from component in angular 2

Santosh Hegde I have an angular2 project which index.htmlcontains a title bar. Other components will be responsible for logging in and displaying other content. I have to display the logo in the title bar, index.htmlonly when the user is logged in. I set a fla

How to pass value to index.html from component in angular 2

Santosh Hegde I have an angular2 project which index.htmlcontains a title bar. Other components will be responsible for logging in and displaying other content. I have to display the logo in the title bar, index.htmlonly when the user is logged in. I set a fla

How to pass value to index.html from component in angular 2

Santosh Hegde I have an angular2 project which index.htmlcontains a title bar. Other components will be responsible for logging in and displaying other content. I have to display the logo in the title bar, index.htmlonly when the user is logged in. I set a fla

How to pass value to index.html from component in angular 2

Santosh Hegde I have an angular2 project which index.htmlcontains a title bar. Other components will be responsible for logging in and displaying other content. I have to display the logo in the title bar, index.htmlonly when the user is logged in. I set a fla

How to pass value from child to parent component in angular 2?

Niranzan I need to get data from child component. The form of my child component is in popu. How to pass full details to parent component. My parent component ts file is import { Component, OnInit } from '@angular/core'; import {MdDialog, MdDialogRef} from

How to pass value from child to parent component in angular 2?

Niranzan I need to get data from child component. The form of my child component is in popu. How to pass full details to parent component. My parent component ts file is import { Component, OnInit } from '@angular/core'; import {MdDialog, MdDialogRef} from

How to pass value from child to parent component in angular 2?

Niranzan I need to get data from child component. The form of my child component is in popu. How to pass full details to parent component. My parent component ts file is import { Component, OnInit } from '@angular/core'; import {MdDialog, MdDialogRef} from

How to pass value from child to parent component in angular 2?

Niranzan I need to get data from child component. The form of my child component is in popu. How to pass full details to parent component. My parent component ts file is import { Component, OnInit } from '@angular/core'; import {MdDialog, MdDialogRef} from

How to pass value from child to parent component in angular 2?

Niranzan I need to get data from child component. The form of my child component is in popu. How to pass full details to parent component. My parent component ts file is import { Component, OnInit } from '@angular/core'; import {MdDialog, MdDialogRef} from

How to pass value from child to parent component in angular 2?

Niranzan I need to get data from child component. The form of my child component is in popu. How to pass full details to parent component. My parent component ts file is import { Component, OnInit } from '@angular/core'; import {MdDialog, MdDialogRef} from

How to pass value from html to component?

User 1231111 I want to pass a static value from html to the component, but it doesn't work, I'm stuck all day... can you give me some glue? thanks Element: import { Component, Input } from '@angular/core'; @Component({ selector: 'rio-hello', template: `<p

Angular 4 pass value from html template to method in component

V. Benavides So I had this problem on Angular 4. I have this button in my html template tag: <td><a class="btn btn-danger" (click)="delete()"><i class="fa fa-trash"></i></a></td> I have a data from *ngFor to each td, so I have {{data.id}} that I can use on th

Angular 4 pass value from html template to method in component

V. Benavides So I had this problem on Angular 4. I have this button in my html template tag: <td><a class="btn btn-danger" (click)="delete()"><i class="fa fa-trash"></i></a></td> I have a data from *ngFor to each td, so I have {{data.id}} that I can use on th

Angular 4 pass value from html template to method in component

V. Benavides So I had this problem on Angular 4. I have this button in my html template tag: <td><a class="btn btn-danger" (click)="delete()"><i class="fa fa-trash"></i></a></td> I have a data from *ngFor to each td, so I have {{data.id}} that I can use on th