要在Angular Material的mat-option中使用span元素来接收值,需要使用mat-select作为父级元素,然后在mat-option中使用span元素来显示值。以下是一个示例解决方法:
{{ option.label }}
import { Component } from '@angular/core';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent {
options = [
{ label: 'Option 1', value: 'option1' },
{ label: 'Option 2', value: 'option2' },
{ label: 'Option 3', value: 'option3' }
];
selectedValue: string;
}
在这个示例中,options数组中的每个对象都有一个label和一个value属性,用于在mat-option中显示和传递值。selectedValue变量用于存储选中的值。
这样,当选择框中的选项发生变化时,span元素会自动显示相应的值。可以通过在组件的代码中访问selectedValue来获取选中的值。
上一篇:Angular(8/9/10)上热模块替换(HMR)无法工作
下一篇:Angular(Cannotreadpropertiesofnull(reading'addControl')-Changeformvalueinchildcomponent)