javascript - Angular Select 添加未定义的选项

标签 javascript html angularjs

我在我的 Controller 中定义了这个 tempScale 对象:

$scope.tempScale = {
    scaleType : [],
    deviations : [],
    intervals : 0
};

它连接到我的 html:

<select id="scales" ng-model="tempScale.scaleType" class="form-control">
    <option value="Manually Calculated" ng-selected="true">Manually Calculated</option>
    <option value="Automatically Calculated">Automatically Calculated</option>
</select>

我在 ng-selected=true 中添加,以便手动计算将是第一个和选定的选项(基本上是默认选项 1),但是,当我运行该页面时,我的 HTML 看起来像:

<select id="scales" ng-model="tempScale.scaleType" class="form-control ng-valid ng-dirty ng-touched">
    <option value="? undefined:undefined ?"></option>
    <option value="Manually Calculated" ng-selected="true" selected="selected">Manually Calculated</option>
    <option value="Automatically Calculated">Automatically Calculated</option>
</select>

为什么那些 ng 类出现在加载时,这个未定义的选项值来自哪里?这不是一个循环,所以我很困惑。

最佳答案

你不需要ng-selected。将 Controller 中的模型设置为 $scope.tempScale.scaleType='Manually Calculated';

One cannot set a default selected item when using ng-model directive with select element. The select element is bind to model field, which data is undefined. What value select should display? Yes, undefined. You try to pass data via markup, it is not an Angular way. Just keep your data in JS model, not in HTML markup.[Ref]

Plunker demo

关于javascript - Angular Select 添加未定义的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34729836/

相关文章:

Javascript 'innerHTML' 方法覆盖复选框选择

javascript:编写可调用对象

JavaScript Keycode 46 是 DEL 功能键还是 (.) 句号?

javascript - 在内容可编辑的 div 中的光标处插入文本

javascript - 从看板移动卡片

angularjs - CORS AngularJS 不通过 POST 或 DELETE 发送 Cookie/凭证,但一切正常

javascript - 与 sibling 一起做两个按钮的互斥复选框()

javascript - 上传文件大小限制为 32kB,我收到错误 "CGI.pm: Server closed socket during multipart read (client aborted?)."

python - 如何将 Angular JS 与 Restful API 结合使用

javascript - Angularjs - 使用 Typeahead 必须显示 2 个值