我很难解释,希望你们了解我的要求并帮助我。
我有一个ng-repeat下面的视图
<div ng-repeat="item in allitems"> {{displaydata}} </div>
我有一个以下数据的控制器
$scope.allitems ={
1:{name:"car",price:"999",qty:8},
2:{name:"bag",price:"127",qty:2},
3:{name:"dog",price:"777",qty:3},
4:{name:"cat",price:"333",qty:4}
}
var displaychoice1 = "{{item.name}}"
var displaychoice2 = "{{item.price}}"
$scope.displaydata = "I want " + displaychoice1 + " which cost " + displaychoice2 ;
我希望用户能够选择“字段”输出到视图,而不是硬编码在视图中显示的内容。
答案 0 :(得分:1)
$scope.allitems
应为array
$scope.allitems = [
{name:"car",price:"999",qty:8},
{name:"bag",price:"127",qty:2},
{name:"dog",price:"777",qty:3},
{name:"cat",price:"333",qty:4}
]
在html上你可以简单地
<div ng-repeat="item in allitems">
I want {{item.name}} which cost {{item.price}}
</div>
修改强>
允许用户选择字段可能类似于
在控制器上:
$scope.displaydata = function(item, field1, field2){
return "I want " + item[field1] + " which cost " + item[field2];
}
在HTML上:
<div ng-repeat="item in allitems">
{{ displaydata(item, 'name', 'price') }}
</div>
您可以调整name
和price
来自您拥有的变量
答案 1 :(得分:0)
您可以使用ngoptions来允许用户选择一个选项:
<select name="mySelect" id="mySelect"
ng-options="item in allitems track by $index"
ng-model="selectedOption"> </select>
I want {{selectedOption.name}} which cost {{selectedOption.price}}