Angular 2: Component Interaction, optional input parameters

前端 未结 3 2039
半阙折子戏
半阙折子戏 2021-02-06 20:39

I have an implementation where parent wants to pass certain data to child component via the use of @Input parameter available at the child component. However, this

3条回答
  •  梦如初夏
    2021-02-06 20:46

    You can use the ( ? ) operator as below

    import {Component,Input} from '@angular/core';
    @Component({
        selector:'child',
        template:`

    Hi Children!

    Alex!` }) export class ChildComponent { @Input() showName?: boolean; constructor() { } }

    The parent component that uses the child component will be as

    @Component({
      selector: 'my-app',
      template: `
        

    Hello {{name}}

    `, }) export class App { name:string; constructor() { this.name = 'Angular2' } }

    LIVE DEMO

提交回复
热议问题