我有带有ng-options的Angular select指令。我还有一个空选项:
<select ng-model="selected_company.current" ng-options="company.name for company in subcontractors">
<option value="">Please select a company...</option>
</select>
但是我希望在列表的末尾有另一个空选项,因此结果列表将是:
我试过了:
<select ng-model="selected_company.current" ng-options="company.name for company in subcontractors">
<option value="">Please select a company...</option>
<option value="">New company...</option>
</select>
但是你可以猜到没有任何事情发生,第二个空选项并没有显示在任何地方。这有什么Angular方式吗?我知道一种替代方案是ng-repeating选项元素,而不是使用ng-options。
答案 0 :(得分:0)
简单的解决方案是我在原始数组中添加另一个元素:
在控制器中
$scope.companies = $scope.subcontractors + [{name: 'New company...'}]
in html
<select ng-model="selected_company.current" ng-options="company.name for company in companies">
<option value="">Please select a company...</option>
</select>
或者更好的是你只需将html修改为以下
<select ng-model="selected_company.current" ng-options="company.name for company in subcontractors + [{name: 'New company...'}] ">
<option value="">Please select a company...</option>
</select>