目前,我正在通过为 angularjs 中的 ng-options 设置默认值而苦苦挣扎。
有人可以帮助我吗?
我从资源中获取选项。
<select id="customer_country" ng-model="customer.country" ng-options="country.name for country in countries" style="width:230px;">
</select>
我目前的解决方法是像这样设置默认值:
$scope.countries = Country.query(function() {
$scope.customer.country = $scope.countries[0];
});
但我认为这不是最好的方法。
我搜索了互联网,但没有找到好的答案。
您将如何分配默认值?
目前我需要这个它没有空值。此外,有时它不会有空值,也不应该使用模型中的值来更新模型。
最佳答案
我自己也遇到了同样的问题,我相信这是解决它的最佳方法。在他们的 documentation for the ng-options directive 中也是这样做的.
关于来自资源默认值的 AngularJS 和 ng-options,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18079278/