简体   繁体   English

遍历json数组以填充下拉列表-angularjs

[英]iterating over a json array to fill a dropdown - angularjs

I have a json array from a json response of this nature 我从这种性质的json响应中得到一个json数组

data {"success":true,"errorCode":0,"data":["kkkk-kkkk 1","oooo-ooooo 2"]}
  1. Item 1 is expected to be >>> kkkk-kkkk 1 项目1预计为>>> kkkk-kkkk 1
  2. Item 2 is expected to be >>> oooo-ooooo 2 项目2预计为>>> oooo-ooooo 2

in angular script controller I am doing this to push the array into the html view 在角度脚本控制器中,我这样做是将数组推入html视图

else if (res.status == 200) { 
    $scope.partners = []; 
    var myJSON = res.data; 
    console.log("data" + JSON.stringify(res.data));
    angular.forEach(myJSON, function (item) { 

    $scope.activities.push(item);

I want to get the json arrays to fill a dropdown of this is a challenge 我想获取json数组以填充下拉菜单,这是一个挑战

<select ng-model="act" formcontrol class="ui fluid dropdown ng-untouched ng-pristine ng-valid" required>
  <option ng-repeat="a in activities" value="{{a}}">{{a}}</option>
</select>

Please assist 请协助

Actually you are iterating the wrong array, you are iterating the object instead. 实际上,您正在迭代错误的数组,而是在迭代对象。

Because in your code: 因为在您的代码中:

console.log("data" + JSON.stringify(res.data));

Will give you: 会给你:

data {"success":true,"errorCode":0,"data":["kkkk-kkkk 1","oooo-ooooo 2"]}

So you need to store res.data.data in your myJSON variable, or the best would be to assign this array directly in your $scope.activities : 因此,您需要将res.data.data存储在myJSON变量中,或者最好是直接在$scope.activities分配此数组:

$scope.activities = res.data.data;

And in your HTML: 在您的HTML中:

<select ng-model="act" formcontrol class="ui fluid dropdown ng-untouched ng-pristine ng-valid"  ng-options="option for option in activities"required>
</select>

使用ng-options

ng-options="item as item for item in activities" 

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

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