【问题标题】:Get drop down from array of object values using meanjs?使用meanjs从对象值数组中获取下拉列表?
【发布时间】:2017-11-04 19:51:20
【问题描述】:
  • 我正在尝试将值作为下拉列表或选择字段获取,它们位于 comments 数组中。

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

例如:

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

  • 我到底在寻找什么 cmets 对象值数组 [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>

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

  • 我只想如果我们选择问题,cmets 选择只过滤这个问题的 cmets...怎么做...谢谢

【问题讨论】:

  • 另一种解决方案?你能描述一下吗?
  • 立即查看我的更新答案。
  • @mani 如果你修复了最后的错误也会给你打勾......

标签: html angularjs meanjs


【解决方案1】:

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;
}
});
<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>

【讨论】:

  • 您好,如果我使用了您的答案并单击更新按钮未更新,我收到以下错误:- Cast to String failed for value "[object Object]" at path "class",如果可能,请帮助我们...
  • 因为它的对象兄弟。如果你想得到 cmets 。然后使用 $scope.commen.commentText .
  • 我应该在哪里使用这个$scope.commen.commentText ?如果可能的话,在控制器(或)html 部分中,您能否展示演示(或)请更新我的 plunker...抱歉打扰您...谢谢(您来自 chennai)如果可以分享您的邮件 ID...谢谢
  • 你到底想要什么。 ?我的演示效果很好。见那里。
  • 如果你解释清楚。我们可以快速轻松地提出建议。
【解决方案2】:

只需更改您的第二个选择,例如:-

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

Working demo

【讨论】:

  • 感谢您的回答,如果我在我的项目中使用了您的回答,它会保存为Booleanlike true 或 false,但我想保存像 7A 或 8A 这样的值......所以你能给其他一些解决方案...谢谢
  • 你可以简单地使用$scope.class.commentText查看更新的plunker
  • 嗨,如果我使用了您的答案并单击更新按钮未更新,我收到以下错误:- Cast to String failed for value "[object Object]" at path "class",如果可能,请帮助我们...
  • 我在模块中的代码:- `class: { type: String, trim: true},在控制器中:- class= req.body.class;并在 html 中:-
  • S.O 就是要获得一些线索永远不要指望复制粘贴你必须相应地修改你的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-03
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
相关资源
最近更新 更多