select指令和两个资源,资源Product.get从中获取包含product.brand项的产品对象,并用作ng-select指令的ngmodel,而第二个资源Brands.query返回用于创建的品牌对象数组ng-select指令的ngOptions参数。
$scope.product = {
"id":"15",
"photo":"15-15-1-cedule-soutez-2015-05-30-11-53-38-5569a4c2bef33.png",
"name":"Hello produkt 3",
"brand":"7",
"price":"29",
"currency":"Kč",
"points":"10"
}
$scope.brands = [
{
"id":"1",
"name":"Marlboro",
"profile":"1",
"rival":"0"
},
{
"id":"7",
"name":"Chesterfield",
"profile":"1",
"rival":"0"}
]
<select data-placeholder="Select brand"
class="w-lg"
ng-options="brand.id as brand.name for brand in brands track by brand.id"
ng-model="product.brand">
<option value=""></option>
</select>
问题是,我无法将此ng-select的选定值设置为第7项切斯特菲尔德。每次都选择第一个空值。我找到了很多针对这些问题的解决方案,但在我的条件下,它们都没有奏效。非常感谢你的帮助。
答案 0 :(得分:1)
请勿在同一表达式中使用select as和track by。它们并非旨在协同工作。
相反,请尝试:
ng-options="brand.id as brand.name for brand in brands"