我在 html 模板中有以下下拉菜单。
<div class="row">
<label class="text-label hint--top-right hint--large" data-hint="{{customer.config.METHOD.DESCRIPTION}}">Calculation Method</label>
<div class="input-select">
<select class="input-select__select" ng-model="customer.attributes.tradingParameters.method">
<option ng-repeat="indicator in customer.config.METHOD.VALUE" ng-selected="customer.attributes.tradingParameters.method == customer.shortenIndicator(indicator)" ng-value="customer.shortenIndicator(indicator)">{{indicator}}</option>
</select>
</div>
</div>
我正在使用 ng-repeat 从 CONSTANT 配置文件生成下拉列表。下面是配置文件。
vm.config = {
'METHOD': {
'VALUE': ['N - No', 'Y- Yes'],
'Description': 'Some description while hovering over'
}
}
在有效响应时,我将带有指示符的响应对象分配给控制器中的attributes属性。然后使用 ng-model 选择合适的值。然而扭曲的是,服务器只发送一个字母指示符,而在下拉列表中我必须有指示符,其中包含一些描述,如“N - No”。
所以我添加了一个 ng-selected 表达式,它将服务器返回的指标与缩短版本进行比较。缩短版本是通过调用控制器中的shortenIndicator方法创建的,该方法在第一次出现空格时拼接指标。
vm.shortenIndicator = function(fullIndicatorValue){
return fullIndicatorValue.slice(0, fullIndicatorValue.indexOf(' '));
};
所以这是一种奇怪的行为,只有下拉列表中的第一个选项在从服务器返回时没有被选中。但是,如果服务器发送第二个或第三个选项,它就会被选中。