2014-11-04 129 views
1

我有一个选择场这样的:如何有条件地设置选定的属性在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> 

正如你看到的,现在我总是选择第一个选项。但实际上,“交易所”有一个“选定”字段,它是一个布尔值,如果该选项应该是选定字段,则设置为真。

你会如何做到这一点?

在此先感谢!

回答

3

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

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

谢谢你,先生!我应该想到这个... – mhanisch 2014-11-04 20:07:51

1

这个问题的一个项目以防万一挣扎,但最后发现,你可以在你的控制器设置设置所选项目: 编辑:我的坏犯了一个错误读我的代码..

$scope.exchangeOption = selecteditemvalue 
+0

谢谢,我认为这也可以,但我现在用Srinivas的方式。 – mhanisch 2014-11-04 20:07:21