我正在尝试将值作为下拉列表或选择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>
寻找第二个解决方案是: -
答案 0 :(得分:1)
只需改变你的第二个选择: -
<select style="margin-left: 20px;" ng-model="class" ng-options="item.commentText for item in question.comments">
</select>
答案 1 :(得分: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;
}
});
&#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;