我已经在 Angular.js 中实现了所选插件,引用 this link
我能够获取该值,现在我想要在所选下拉列表中预先选择所选值。
谁能告诉我一种简单的方法来做到这一点。示例代码:
<select name="SelectedName" id="SelectedName" data-placeholder="Choose a Name..." chosen
ng-model="Info.SelectedNameModel"
ng-options="jd.Name for jd in Info.List"
class="form-control chosen-select"></select>
在Js代码中
.directive('chosen', function() {
var linker = function (scope, element, attr) {
// update the select when data is loaded
scope.$watch('Info.List', function (oldVal, newVal) {
element.trigger('chosen:updated');
});
element.chosen();
};
return {
restrict: 'A',
link: linker
};
});
示例列表:{"Name":"PQR"},{"Name":"LMN"}
在数据库中,我将唯一的值存储为'LMN'
因此,当下拉列表加载时,我希望显示为已选择的 LMN。
如果有人已经这样做了,请告诉我。
最佳答案
将 ng-options 更新为 jd.Name for jd in Info.List track by jd
你可以改成这样:
<select name="SelectedName" id="SelectedName" data-placeholder="Choose a Name..." chosen
ng-model="Info.SelectedNameModel"
ng-options="jd.Name for jd in Info.List track by jd"
class="form-control chosen-select"></select>
关于javascript - Angular js + 选择插件 + 将值设置为在下拉列表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32520579/