I have a project where I'm tasked with collecting multiple user-selected items and sending them to the server. On my view, there's a list that users can click to select items. Here's a snippet of my HTML:
HTML
<div ng-repeat="topicList in searchCtrl.topic">
<div ng-repeat="topicTerm in topicList">
<p>{{topicTerm.number}}  {{topicTerm.name}}</p>
<div ng-repeat="subTopic in topicTerm.subTopics">
<a href="" ng-click="searchCtrl.select($event)">{{subTopic.number}}  {{subTopic.name}}</a>
</div>
</div>
</div>
The anchor tag allows users to click on items, while also giving each item a unique ID for collection in an array or variable. This collection will be sent to the server via form submission.
This is what my controller looks like:
JavaScript Controller
angular.module('myApp').controller("searchController", function($log, searchService, $scope){
var self = this;
self.initializeSearch = function(){
self.searchEntry =
{
"contact":{
"person": "",
"organization": ""
},
"request": {
"input": "",
"language": "en"
},
"topicIds": []
};
// The POST request must looks like above
The goal is to collect the clicked subTopics IDs in an Array "topicIds : []" so that the POST request mentioned above can be successfully sent. The searchService is an Angular service used to fetch topics from the server and handle POST requests.
This is how my JSON data is structured:
JSON API
{
"TopicList" :[
{
"id": "798790fa-78c8-4f00-8179-9e70f40adb14",
"name": "Topic1",
"number": 1.0,
"subTopics": [
]
},
...
]
}
How can I create a function or array that collects the IDs through an ng-click event?
Thank you in advance.