在选择标签上有ng-model问题,但没有其他问题
Having issues with ng-model on select tag, but nowhere else
我有一堆ng-model语句,它们都在工作,除了选择标签上的那个。本质上,想象一个带有设备列表的页面,当其中一个被点击时,这个函数运行:
/* Opens the unit popup page for TX units */
this.openTxUnitPopup = function (row) {
var txUnitForPopup = myScope.txUnits.find(function (unit) {
return unit.mac == row.entity.mac;
});
$scope.config.txUnitForPopup = txUnitForPopup.clone();
$scope.config.showTxPopup = true;
};
该函数的目的是为模态对话框设置一些数据。下面是带有ng-model语句的标记。同样,当模态弹出窗口出现时,除了选择框之外,所有内容都将根据模型正确填写。没有选择通道。我可以在调试器中确认通道正在通过选择框更改,如果我选择一个通道,然后查看它已更改的txUnitForPopup.channel
。然而,在视图中,选择框从来没有像其他表单元素那样,在modal第一次出现时预先填充一个被选中的选项。
<div class="form-column">
<span class="info-label">Part:</span>
<div class="my-form-control">{{config.txUnitForPopup.part}}</div>
<span class="info-label">MAC:</span>
<div class="my-form-control">{{config.txUnitForPopup.mac}}</div>
<span class="info-label">Channel:</span>
<select class="my-form-control" ng-model="config.txUnitForPopup.channel">
<option
ng-repeat="unit in myScope.txUnits"
value="{{unit.channel}}">{{unit.channel}}
</option>
</select>
<span class="info-label">Name:</span>
<input class="my-form-control" ng-model="config.txUnitForPopup.info.name">
<span class="info-label">Manufacturer:</span>
<input class="my-form-control" ng-model="config.txUnitForPopup.info.manufacturer">
<span class="info-label">Model:</span>
<input class="my-form-control" ng-model="config.txUnitForPopup.info.model">
<span class="info-label">Serial #:</span>
<input class="my-form-control" ng-model="config.txUnitForPopup.info.serial">
</div>
您需要使用ng-selected将先前选择的值设置为像这样的下拉列表
<select class="my-form-control" ng-model="config.txUnitForPopup.channel">
<option
ng-repeat="unit in myScope.txUnits"
ng-selected="unit.channel == config.txUnitForPopup.channel"
value="{{unit.channel}}">{{unit.channel}}
</option>
</select>
您可能想要在选择中尝试ng-options
指令而不是ng-repeat
。
<select ng-options="unit in myScope.txUnits"
ng-model="config.txUnitForPopup.channel">
</select>
请再看一下ng-options。这是官方文档中的一个plunkr(有一些小修改)
(function(angular) {
'use strict';
angular.module('defaultValueSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
availableOptions: [
{id: '1', name: 'Option A'},
{id: '2', name: 'Option B'},
{id: '3', name: 'Option C'}
],
selectedOption: {id: '3', name: 'Option C'} //This sets the default value of the select in the ui
};
}]);
})(window.angular);
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-select-with-default-values-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="defaultValueSelect">
<div ng-controller="ExampleController">
<form name="myForm">
<label for="mySelect">Make a choice:</label>
<select name="mySelect" id="mySelect"
ng-options="option.name for option in data.availableOptions track by option.id"
ng-model="data.selectedOption"></select>
</form>
<button ng-click="data.selectedOption = data.availableOptions[1]">
Set option B</button>
<hr>
<tt>option = {{data.selectedOption}}</tt><br/>
</div>
</body>
</html>
<!--
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->
相关文章:
- 如何消除代码中的未定义和其他问题
- 为什么不'我的窗口滚动事件根本没有启动.其他答案没有解决问题
- 正则表达式匹配模式或匹配其他所有内容 - 匹配优先级问题
- document.write和document.innerHTML以及其他javascript问题之间有什么区别
- 如果其他问题,则使用Javascript
- 这很难简明扼要地说.但是我的 if/else javascript 有问题.“如果”在“其他”发生之后不会发生
- 与其他浏览器相比,如何在 safari 或 mac 中解决字体粗细、文本过于粗体的问题
- 虽然循环不起作用(在其他类似问题中找不到答案)
- j查询验证模糊文本区域与其他输入问题
- 日期选取器问题,尝试提交除日期值之外的其他参数
- Drupal网站页面缓慢(这与同一主题的其他问题不同)
- 在定位选择器(或其他东西)时遇到问题
- 绝对元素粒子重叠其他元素的问题
- 更新按钮文本和其他问题
- 开始使用Angular JS-Structure和其他问题
- 执行用户脚本时不存在篡改或其他问题
- jQueryUI-拖放问题-拖动的元素位于其他DOM元素之后
- backstretch导致chrome中的其他背景图像出现问题
- 网络音频API质量差和Safari中的其他问题
- if中的问题&其他的事情