将选定的md-select值传递给函数

时间:2017-10-08 18:56:54

标签: angular angular-material

       <md-select  i18n-placeholder="language|languages list @@lang" (change)="setLanguage(this.value)" 
           placeholder="Lang">
            <md-option value="Lt-uz">
                O'z
            </md-option>
            <md-option value="ru">Ru</md-option>
        </md-select>

上面的代码传递了未定义的值。如何将所选值传递给函数?

1 个答案:

答案 0 :(得分:0)

1-您不应该在模板中使用它。

2-你的md-select没有绑定到类变量。

试试这个:

<md-select  i18n-placeholder="language|languages list @@lang" 
           [(ngModel)]="selectedLanguage" 
           (change)="setLanguage()" 
           placeholder="Lang">
    <md-option value="Lt-uz">
         O'z
    </md-option>
    <md-option value="ru">Ru</md-option>
</md-select>

在您的ts代码中:

export class YourTsClass{
   selectedLanguage:string = '';
   //....
   setLanguage(){
     //When this function will be called, selectedLanguage will have the selected value from md-select
    //You can then use this.selectedLanguage to access its value

   }            
}