如何使用angularjs在ng-options中使用数组值?

时间:2017-10-25 13:06:43

标签: html angularjs meanjs

我正在尝试将值作为下拉列表或选择comments数组中的字段。我在comments中尝试commentText drop down数组ng-option,我怎样才能在ng-option中执行此操作? My Plunker

例如:

我首先在没有数组值的情况下进行下拉ng-options="item.comments.commentText for item in questions"。现在我在下拉列表中查找[commentText“:”7A“]的注释数组值。我该怎么做。

我试过 $scope.questions = [ { "_id": "59df6c37b6748bc809050699", "user": { "_id": "59df6a76b6748bc809050697", "profileImageURL": "modules/users/client/img/profile/default.png" }, "__v": 1, "comments": [ { "created": 1507897712831, "email": "ms@e21designs.com", "name": "Maniselvam selvam", "link": "https://drive.google.com/drive/u/0/folders/0B5c-p1bvkfS9REJHMGhMY1BTV1k", "commentText": "7A" } ], "questionid": "", "created": "2017-10-12T13:20:55.383Z" } ] 它不适合我。如何解决?

我的数据:

    <label>1. Without array value</label>
  <select ng-model="class"  ng-options="item.title for item in questions">
  </select>
  </div>

  <div style="margin-top: 11px;">
  <label>2. With comments array value</label>
  <select style="margin-left: 20px;" ng-model="class"  ng-options="item.comments.commentText for item in questions">
  </select>
  </div>

我的Html:

ExecuteQuery

1 个答案:

答案 0 :(得分:0)

编辑: 这就是我编辑的内容: 我将所有问题的所有注释合并到一个数组中,并为每个注释添加了问题ID(就像我们可以使用问题过滤(第一个选择的ng模型)) 我希望它有所帮助

  <div ng-controller="MyCntrl">
    <div>
      <label>1. Without array value</label>
      <select ng-model="question" ng-options="item.title for item in questions">
      </select>
    </div>

    <div style="margin-top: 11px;">
     <label>2. With comments array value</label>
     <select style="margin-left: 20px;" ng-model="class" ng-options="item.commentText for item in filteredComments()">
     </select>
    </div>

他的控制器脚本(在评论中更改了question_id以获得一个实际的例子)

$scope.comments = [];
  $scope.question = {};
  angular.forEach($scope.questions, function(item) {
    angular.forEach(item.comments, function(comment) {
      comment.question_id = item._id;
      $scope.comments.push(comment);
    });
  });

  $scope.filteredComments = function() {
    if ($scope.question._id) {
      var filteredComments = [];
      angular.forEach($scope.comments, function(item) {
        if (item.question_id == $scope.question._id) {
          this.push(item);
        }
      }, filteredComments);

      return filteredComments;
    } else {
      return $scope.comments
    };
  }

http://plnkr.co/edit/UzP9fnsxxPfngVRAW0c0?p=preview