使用meanjs从对象值数组中下拉?

时间:2017-11-02 12:23:50

标签: html angularjs meanjs

  • 我正在尝试将值作为下拉列表或选择comments数组中的字段。

  • 我正在尝试comments中的commentText对象drop down数组,我该如何在ng-option中执行此操作? My Plunker

例如:

  • 我首先下载了ng-option,没有像ng-options="item.title for item in questions"那样的数组值。

  • 我正在寻找的对象值[commentText": "7A"]的注释数组在下拉列表中我该怎么做。

  • 我试过ng-options="item for item.comments.commentText in questions"它不适合我。如何解决?

我的数据:

$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"
}
]

我的Html:

  <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 for item.comments.commentText in questions">
  </select>
</div>

寻找第二个解决方案是: -

  • 我只想要,如果我们选择问题,评论选择过滤此问题的评论...如何做到这一点...谢谢

2 个答案:

答案 0 :(得分:1)

只需改变你的第二个选择: -

 <select style="margin-left: 20px;" ng-model="class"  ng-options="item.commentText  for item in question.comments">
  </select>

Working demo

答案 1 :(得分:1)

&#13;
&#13;
var myApp = angular.module('exApp',[]);

myApp.controller('myctrl', function($scope){
 $scope.questions = [
{
"_id": "59df6c37b6748bc809050699",
"user": {
"_id": "59df6a76b6748bc809050697",
"profileImageURL": "modules/users/client/img/profile/default.png"
},
"__v": 1,
"openeyers": [],
"openeyes": 0,
"upvoters": [],
"upvotes": 0,
"isLiked": false,
"users": [],
"comments": [
{
"created": 1507897712831,
"email": "ms@e21designs.com",
"name": "Maniselvam selvam",
"link": "https://drive.google.com/drive/u/0/folders/0B5c-p1bvkfS9REJHMGhMY1BTV1k",
"commentText": "7A"
},
{
"created": 1507897712831,
"email": "ms@e21designs.com",
"name": "Maniselvam selvam",
"link": "https://drive.google.com/drive/u/0/folders/0B5c-p1bvkfS9REJHMGhMY1BTV1k",
"commentText": "9A"
},
{
"created": 1507897712831,
"email": "ms@e21designs.com",
"name": "Maniselvam selvam",
"link": "https://drive.google.com/drive/u/0/folders/0B5c-p1bvkfS9REJHMGhMY1BTV1k",
"commentText": "10A"
}
],
"questionid": "",
"title": "Silver jublie",
"created": "2017-10-12T13:20:55.383Z"
},
{
"_id": "59df6c37b6748bc809050699",
"user": {
"_id": "59df6a76b6748bc809050697",
"displayName": "Maniselvam selvam",
"dob": "1991-05-10T07:00:00.000Z",
"profileImageURL": "modules/users/client/img/profile/default.png"
},
"__v": 1,
"openeyers": [],
"users": [],
"comments": [
{
"created": 1507897712831,
"email": "ms@e21designs.com",
"name": "Maniselvam selvam",
"link": "https://drive.google.com/drive/u/0/folders/0B5c-p1bvkfS9REJHMGhMY1BTV1k",
"commentText": "8A"
}
],
"questionid": "",
"title": "Public School",
"created": "2017-10-12T13:20:55.383Z"
}
];
$scope.editClass = function(section){
$scope.sectionwithObject = section;
$scope.sectionOnly=section.commentText;
$scope.newLink = section.link;
}
});
&#13;
<html>
  <head>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>

  </head>
  <body ng-app="exApp" ng-controller="myctrl">

<div>
  <div>
  <label>1. Without array value</label>
  <select ng-model="ques"  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="commen"  ng-options="item as item.commentText for item in ::ques.comments" ng-change="editClass(commen)">
  </select>{{commen}}
 <br><br>
  <div>
    <input type="text" ng-model="sectionwithObject.commentText"/><br><br>
     <input type="text" ng-model="sectionOnly"/>
  </div><br>
  <div>
    <lable> Link</lable>
    <input type="text"  ng-model="sectionwithObject.link" /><br><br>
        <input type="text"  ng-model="newLink" />
  </div>
  </div>
  
 

</div>


  </body>
</html>
&#13;
&#13;
&#13;