简体   繁体   English

如何在Angular JS列表框中设置值

[英]How to set value in Angular JS Listbox

How to set the item in Angular JS Listbox. 如何在Angular JS列表框中设置项目。 This should be shown as selected in Listbox. 这应该显示为在列表框中选择。

HTML 的HTML

<select multiple ="multiple" style="width:180px" ng-model="md_fruitpref" ng-options="fruit.Id as fruit.Name for fruit in Fruits"></select>

Controller 控制者

//Controller to set the items in Fruit 
$scope.Fruits = [{
    Id: 1,
    Name: 'Apple'
}, {
    Id: 2,
    Name: 'Mango'
}, {
    Id: 3,
    Name: 'Orange'
}, {
    Id: 4,
    Name: 'Guava'
}, {
    Id: 5,
    Name: 'Banana'
}, {
    Id: 6,
    Name: 'Watermellon'
}];

This will fill the Listbox with the various fruits. 这将使列表框充满各种水果。

Now I want to show the webpage with all the fruits in the Listbox but selected with Apple and Watermellon. 现在,我想显示带有列表框中所有水果但被Apple和Watermellon选择的水果的网页。 So I do this but it does not help. 所以我这样做,但这无济于事。

$scope.md_fruitpref= [{
    Id: 1,
    Name: 'Apple', 
    Id: 6,
    Name: 'Watermellon'    
}];

How to set value in Angular JS Listbox? 如何在Angular JS列表框中设置值?

Try: 尝试:

// array with 2 elements (each one an object with the same properties)
$scope.md_fruitpref= [{
    Id: 1,
    Name: 'Apple'
}, {
    Id: 6,
    Name: 'Watermellon'    
}];

The structure you currently have does not work because you only have one element in your md_fruitpref array, and the object is invalid because it uses the same keys twice. 当前使用的结构不起作用,因为md_fruitpref数组中只有一个元素,并且该对象无效,因为它两次使用相同的键。

Finally I found the answer to this. 最后,我找到了答案。 It will be :- 这将是 :-

$scope.md_fruitpref= [1,6];

After this line soon you will see the selection in Listbox 在这行之后,您很快就会在列表框中看到选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM