Set default option in mat-select

后端 未结 11 1730
北恋
北恋 2020-11-30 08:31

I have a simple select option form field in my Angular material project:

component.html

  
    

        
11条回答
  •  没有蜡笔的小新
    2020-11-30 08:50

    Using Form Model (Reactive Forms)

    --- Html code--
    
    -- {{category.name}}

    ----ts code ---

    ngOnInit() {
    
            this.patientCategory = this.fb.group({
                patientCategory: [null, Validators.required]
            });
    
          const toSelect = "Your Default Value";
          this.patientCategory.get('patientCategory').setValue(toSelect);
        }
    

    With out form Model

    --- html code --
    
      Select an option
      
        None
        Option 1
        Option 2
        Option 3
      
    
    

    ---- ts code -- selected = 'option1'; Here take care about type of the value assigning

提交回复
热议问题