我正在将键推送到一个数组,我正在迭代它以获取dropdown中的选项。创建(键,值)对,数组中的键和选项是相同的。当我们更改下拉列表中的选项时,相应的值应以HTML格式呈现。
以下是代码:
$scope.viewItemKeys = [pencils, pens, books];
$scope.viewItemObj = {
"pencils": {
"0": {},
"1": {},
"2": {}
},
"pens": {
"0": {},
"1": {},
"2": {},
"3": {},
"4": {}
},
"5": {},
"6": {},
"7": {}
},
"books": {
"0": {},
"1": {},
"2": {},
"3": {},
"4": {}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<select id="selId" ng-options="key as value for (key , value) in viewItemkeys" ng-model="selectedItem" ng-change='onItemChange()'></select>
如果我选择了关于键的笔(即1到7,内部笔),请帮助我,我需要渲染{}值
答案 0 :(得分:1)
看看这个小提琴手here
你的viewItemObj有点不对劲
$scope.viewItemObj = {
"pencils": {
"0": {},
"1": {},
"2": {}
},
"pens": {
"0": {},
"1": {},
"2": {},
"3": {},
"4": {},
"5": {},
"6": {},
"7": {}
},
"books": {
"0": {},
"1": {},
"2": {},
"3": {},
"4": {}
}
};
那里有一个额外的'}'。 我修改了HTML如下
<select id="selId" ng-options="value as key for (key , value) in viewItemObj" ng-model="selectedItem" ng-change='onItemChange()'></select><br/>
Selected Item - {{selectedItem | json}}
这将呈现您选择的键的值 如果您正在寻找,请告诉我。
修改强>
根据您的评论,我为您here创建了一个新的FIddle。这个新的小提琴将显示所选对象的值。我稍微修改了一下HTML:
<div ng-controller="MyCtrl">
<select id="selId" ng-options="value as key for (key , value) in viewItemObj" ng-model="selectedItem" ng-change='onItemChange()'></select>
<div ng-repeat="item in selectedItem">
{{item.Name}}
</div>
</div>
我还在你的对象中添加了一些虚拟数据
$scope.viewItemObj = {
"pencils": {
"0": {
Name: "Camel"
},
"1": {
Name: "Nataraj"
},
"2": {
Name: "Space"
}
},
"pens": {
"0": {
Name: "Camel"
},
"1": {
Name: "Parker"
},
"2": {
Name: "Fountain"
},
"3": {
Name: "Dealer"
},
"4": {
Name: "Gel"
},
"5": {
Name: "Inker"
},
"6": {
Name: "Mountain View"
},
"7": {
Name: "Decor"
}
},
"books": {
"0": {
Name: "The Da Vinci Code"
},
"1": {
Name: "How to kill a Mocking Bird"
},
"2": {
Name: "Tooth and Nails"
},
"3": {
Name: "A song of Ice and Fire"
},
"4": {
Name: "Dance with the Dragons"
}
}
};