简体   繁体   English

以异步方式角材料 md-select 加载选项

[英]Angular Material md-select load options in async way

I need to load select's options asynchronously ( through a service), using the Angular Material md-select component.我需要使用 Angular Material md-select组件异步加载 select 的选项(通过服务)。

Actually, I use a click event to load data.实际上,我使用click事件来加载数据。 It works but I need to click the select twice to show the options.它有效,但我需要单击两次选择以显示选项。 That it's a problem.这是一个问题。

The expected behavior is shown at this link (AngularJs Material)预期行为显示在此链接(AngularJs 材料)

The actual behavior is shown at this link .实际行为显示在此链接中

Is Async options' loading supported by md-select ? md-select是否支持异步选项的加载?

The reason you need to click twice is because when you first click, there are no options in the select control and so it doesn't try and open the panel.您需要单击两次的原因是因为当您第一次单击时,选择控件中没有选项,因此它不会尝试打开面板。 Then your async method loads the options into the DOM and on the next click it will open.然后您的异步方法将选项加载到 DOM 中,并在下次单击时打开。

In order to deal with this, you must always include at least one <mat-option> in your <mat-select> .为了解决这个问题,您必须始终在<mat-select>中至少包含一个<mat-option> <mat-select> You can add a disabled <mat-option> with a <mat-spinner> showing that the data is loading for example.例如,您可以添加一个禁用的<mat-option>和一个<mat-spinner>显示数据正在加载。

Here the most simple example of that. 这是最简单的例子。 This is not the best approach... see below.这不是最好的方法……见下文。

However, this still uses the click event which isn't the best approach.但是,这仍然使用 click 事件,这不是最好的方法。 If you put the click event on the <mat-select> there are spots where you can click on the control but your click event wont trigger even though the dropdown panel still opens (places like the floating label area).如果您将点击事件放在<mat-select>上,您可以在某些地方点击控件,但即使下拉面板仍然打开(例如浮动标签区域),您的点击事件也不会触发。 You could put the click event on the <mat-form-field> but then there will be places where you can click and trigger the click event but the dropdown panel wont open (places like the hint/error text area).您可以将点击事件放在<mat-form-field>但随后会有一些地方您可以点击并触发点击事件,但下拉面板不会打开(提示/错误文本区域之类的地方)。 In both cases you lose keyboard support.在这两种情况下,您都失去了键盘支持。

I would suggest using the <mat-select> openChanged event instead of a click event.我建议使用<mat-select> openChanged事件而不是单击事件。 This has its own quirks too but they are manageable.这也有其自身的怪癖,但它们是可控的。

Here is an example using the openChanged event . 这是一个使用 openChanged 事件的示例 I also made the component more robust overall.我还使组件整体更加健壮。

I also made a version that uses the placeholder element to show the spinner instead of using a disabled mat-option. 我还制作了一个版本,它使用占位符元素来显示微调器,而不是使用禁用的垫子选项。 This required View Encapsulation to be turned off.这需要关闭视图封装。

Note: In my example, the service can return different data depending on the circumstances.注意:在我的示例中,服务可以根据情况返回不同的数据。 To simulate this my fake service keeps track of how many requests you send it and changes the options returned accordingly.为了模拟这一点,我的假服务会跟踪您发送的请求数量,并相应地更改返回的选项。 So I have to set the option list back to empty and clear the formControl 's value every time the list is opened.因此,每次打开列表时,我都必须将选项列表设置回空并清除formControl的值。 I save the selected value before clearing the formControl so that if the service returns a list with the same item I can automatically reselect the value.我在清除formControl之前保存选定的值,以便如果服务返回具有相同项目的列表,我可以自动重新选择该值。 If you only need to load the options once, then you would want to modify the code a bit to only load the options the first time the user opens the select.如果您只需要加载一次选项,那么您可能需要稍微修改代码以仅在用户第一次打开选择时加载选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM