我有这样的数据:
"RequirementLevels": {
"285960000":"Black",
"285960001":"Green",
"285960002":"Blue",
"285960003":"Purple"
}
我有一个像这样的选择:
<select ng-options="key as value for (key , value) in Variables.Template.RequirementLevels"
ng-model="Variables.EditingNode.RequirementLevel"
ng-model-options="{ debounce: 300 }"></select>
<span>"{{Variables.EditingNode.RequirementLevel}}"</span>
请注意选择选项中每个值前面的字符串:
。使用下面的代码我没有得到值中的string:
。
<select ng-model="Variables.EditingNode.RequirementLevel">
<option ng-repeat="(key, value) in Variables.Template.RequirementLevels" value="{{key}}">{{value}}</option>
</select>
我的问题是为什么我会在 ng-option 列表中得到 string:
以及如何让它消失。
更新,因为评论:我想更改此设置的原因是因为当值中包含 string:
时,ng-model 值不起作用。我认为这是因为它与模型中的“123”选项集中的“string:123”不匹配。
更新2
这是创建选择的 Html。
<div class="form-group input-group">
<label for="ReviewDone">Requirement level</label>
<select ng-options="key as value for (key , value) in Variables.Template.RequirementLevels track by key"
ng-model="Variables.EditingNode.RequirementLevel"
ng-model-options="{ debounce: 300 }"></select>
<span>"{{Variables.EditingNode.RequirementLevel}}"</span>
</div>
这将创建带有选项的选择,但不会选择默认值。然而,它下面显示 ng-model 变量内容的跨度工作正常,并打印选项列表中的数字“285960002”,因此应该从列表中选择它。
上面的代码生成以下 HTML:
<div class="form-group input-group">
<label for="ReviewDone">Requirement level</label>
<select class="ng-pristine ng-untouched ng-valid" ng-model="Variables.EditingNode.RequirementLevel" ng-model-options="{ debounce: 300 }" ng-options="key as value for (key , value) in Variables.Template.RequirementLevels track by key"><option selected="selected" value="?"></option><option value="285960000" label="Black">Black</option><option value="285960001" label="Green">Green</option><option value="285960002" label="Blue">Blue</option><option value="285960003" label="Purple">Purple</option></select>
<span class="ng-binding">"285960002"</span>
</div>
此外,在选择中选择不同的值也会更改范围中的值,因此它会更新该值,但在加载时不会读取它。
最佳答案
如果您想让这个附加字符串按照您的要求消失,请使用按键跟踪。
<select ng-options="key as value for (key , value) in Variables.Template.RequirementLevels track by key"
请参阅 plunker 以供引用
关于angularjs - ng-options 添加 "string:"值并且不更新 ng-model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37791454/