首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >AngularJS -获取模型的选定<option>的值

AngularJS -获取模型的选定<option>的值
EN

Stack Overflow用户
提问于 2015-06-30 03:29:01
回答 2查看 983关注 0票数 2

我在使用ng-model和ng-selected时遇到了问题。在一开始,ng-model为空,并且选择了正确的值。因此,如果您提交表单:

{ resolution : undefined, desiredFps : 30 }

这是不正确的。

因此,我希望<select>的模型根据ng-selected属性采用选定的值。更新所选值以及使用该值更新模型的正确方法是什么?

<form novalidate name="preferencesForm" 
                 ng-submit="submitPreferencesForm(BEPreferencesForm)"
                 ng-controller="UserPreferencesFormController">
   <label for="resolution">Choose the resolution : </label>
   <br/>
   <select name="resolution" id="resolution"
           ng-model="BEPreferencesForm.resolutionId"
           ng-change="buttonDisabled = False">
      <option value="1" ng-selected="user.screenWidth ==  800 && user.screenHeight == 600">800x600</option>
      <option value="2" ng-selected="user.screenWidth == 1024 && user.screenHeight == 768">1024x768</option>
      <option value="3" ng-selected="user.screenWidth == 1280 && user.screenHeight == 720">1280x720</option>
      <option value="4" ng-selected="user.screenWidth == 1280 && user.screenHeight == 768">1280x768</option>
      <option value="5" ng-selected="user.screenWidth == 1360 && user.screenHeight == 768">1360x768</option>
      <option value="6" ng-selected="user.screenWidth == 1600 && user.screenHeight == 900">1600x900</option>
      <option value="7" ng-selected="user.screenWidth == 1768 && user.screenHeight == 992">1768x992</option>
      <option value="8" ng-selected="user.screenWidth == 1920 && user.screenHeight == 1080">1920x1080</option>
   </select>
</form>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-30 03:51:27

有一种Angular的方式,你应该使用ng-options指令。简化示例:

<select ng-model="BEPreferencesForm.resolutionId"
        ng-options="item.id as item.name for item in items">
</select>

在控制器中:

$scope.items = [
    { id: 1, name: '1024x768' },
    { id: 2, name: '1280x768' }
];

// Resolution with id = 2 will be selected by default:
$scope.BEPreferencesForm = {
    resolutionId: 2
};
票数 0
EN

Stack Overflow用户

发布于 2015-06-30 03:56:20

你应该看看文档:https://docs.angularjs.org/api/ng/directive/select

ng-model在被选中时采用选项值。如果屏幕分辨率为800x600,您的BEPreferencesForm.resolutionId将等于1。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31123963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档