来自资源默认值的 AngularJS 和 ng-options

标签 angularjs

目前,我正在通过为 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/

相关文章:

javascript - 类(class)评价时间太长。 AngularJS

javascript - 从 Angular Directive(指令)返回数据

AngularJS - 使用服务作为模型,ng-repeat 不更新

angularjs - 多个验证指令不起作用

javascript - 上传文件 Angular

javascript - Aurelia 是否有 AngularJS $watch 替代方案?

javascript - 如何兑现 promise ?

javascript - $watch 不适用于 angularjs 中的指令属性

angularjs - Protractor 在中继器内查找元素

AngularJS 代码不适用于 NodeJS