angular-material compareWith select默认选中某一项
Angular引入了一个方便的指令来帮助您比较选择的选项。让我们看看这个指令是如何工作的。将此指令添加到您的选择中:
<select [compareWith]="byAnimal" [(ngModel)]="selectedAnimal">
  <option *ngFor="let animal of animals" [ngValue]="animal">
    {{animal.type}}
  </option>
</select>
compareWith在您的组件中定义方法:
byAnimal(item1,item2){ return item1.type == 'dog' }
返回true的比较将成为选定的选项。

 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号