Angular 5 Insert dynamic input attributes

后端 未结 3 349
青春惊慌失措
青春惊慌失措 2021-01-24 09:44

I want to insert dynamically attributes to an input html tag, but I don\'t know to to do this:

I\'ve got this code from component side:

import { Componen         


        
3条回答
  •  迷失自我
    2021-01-24 10:31

    If you want to dynamically change the attributes of a single tag, I would recommend you use @ViewChild. For example,

    import { Component, AfterViewInit, ElementRef } from '@angular/core';
    
    @Component({
      selector: 'app-transclusion',
      template: `
        
        `,
      styleUrls: ['./transclusion.component.css']
    })
    export class TransclusionComponent implements AfterViewInit {
    
      @ViewChild('foobar') foobar: ElementRef;
    
      constructor() { }
    
      ngAfterViewInit() {
        this.foobar.nativeElement.value = 'foobar';
        // change other values of element
      }
    }
    

提交回复
热议问题