如何在AngularJS中有条件地设置选定的属性(How to conditionally set selected property in AngularJS)

我有一个像这样的选择字段:

<select ng-init="exchangeOption = widget.content.exchange[0]" ng-Model="exchangeOption" ng-options="exchangeOption.title for exchangeOption in widget.content.exchange" ng-change="internalLink(exchangeOption.url)"> </select>

如你所见,我现在总是选择第一个选项。 但实际上“交换”有一个“选中”字段,它是一个布尔值,如果该选项应该是选中的那个,则设置为true。

你会怎么做?

提前致谢!

I have a select-field like this:

<select ng-init="exchangeOption = widget.content.exchange[0]" ng-Model="exchangeOption" ng-options="exchangeOption.title for exchangeOption in widget.content.exchange" ng-change="internalLink(exchangeOption.url)"> </select>

As you see, right now I always select the first option. But actually the "exchanges" have a "selected"-field, which is a boolean, set to true if that option should be the selected one.

How would you do this?

Thanks in advance!

最满意答案

您可以通过过滤交换来编写返回所选交换选项的方法。

<select ng-init="exchangeOption = getSelectedExchange()" ng-Model="exchangeOption" ng-options="exchangeOption.title for exchangeOption in widget.content.exchange" ng-change= "internalLink(exchangeOption.url)"> </select>

You can write a method to return selected exchange option by filtering the exchanges.

<select ng-init="exchangeOption = getSelectedExchange()" ng-Model="exchangeOption" ng-options="exchangeOption.title for exchangeOption in widget.content.exchange" ng-change= "internalLink(exchangeOption.url)"> </select>

更多推荐