Pass variable to custom component

后端 未结 2 1895
感情败类
感情败类 2020-12-08 18:36

I have my custom component:

@Component({
    selector: \'my-custom-component\',
    templateUrl: \'./my-custom-component.html\',
    styleUrls: [\'./my-custo         


        
2条回答
  •  一生所求
    2020-12-08 19:09

    You need to add Input property to your component and then use property binding to pass value to it:

    import { Component, Input } from '@angular/core';
    
    @Component({
        selector: 'my-custom-component',
        templateUrl: './my-custom-component.html',
        styleUrls: ['./my-custom-component.css']
    })
    export class MyCustomComponent {
    
        @Input()
        customTitle: string;
    
        constructor() {
            console.log('myCustomComponent');
        }
    
        ngOnInit() {
            console.log(this.customTitle);
        }
    }
    

    And in your template:

    
    

    For more info, check out this page.

提交回复
热议问题