Generating a JSON array from dynamically added list items using Angular 2: A step-by-step guide

I'm currently learning Angular 2 and I have a question about auto-complete functionality. I have an input field that suggests categories when searching, and when I click on one of the suggestions, it adds a new category to a list called category-tags using JQuery.

Now, I want to store these categories in a JSON array named "categories" and post them to a database.

Here's the code for the auto-complete input:

<h6>Add category</h6>
        <div class="col-lg-12 padding0">
            <input [formControl]="term" autocomplete="off" type="text" class="form-control" placeholder="Type Category Name" id="product-category-input">
            <div class="catsuggestions col-lg-12 padding0">
                <ul>
                    <li (click)="addCategory()" *ngFor="let item of items | async"><a href="#!" data-catid="item.categoryId">{{item.categoryName}}</a></li>
                </ul>
            </div>
        </div>

List where items are added:

<ul class="category-tags"></ul>

I need help in creating a JSON array from dynamically added list items. Thank you!

Answer №1

Utilize the json pipe for handling those types of situations. It typically functions seamlessly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: An unexpected identifier was found within the public players code, causing a SyntaxError

As a newcomer to jasmine and test cases, I am endeavoring to create test cases for my JavaScript code in fiddle. However, I'm encountering an error: Uncaught SyntaxError: Unexpected identifier Could you guide me on how to rectify this issue? Below is ...

Angular2 experiencing issues loading boot.js file

Whenever I try to launch my project directly from file:///C:/wamp/www/ngProject/index.html or through WAMP at http://localhost/ngproject/, it always gets stuck on the boot.js file. Interestingly, in development mode with npm-start, everything works just f ...

Angular 7 - Personalized Name for Formbuilder's Dynamic Form

Currently, I am developing a form builder with Angular: return this.fb.group( { myaccount: [ '', [Validators.required]] } ); When an error message is thrown for an element, I iterate through the form controls and disp ...

What is the best way to update the current route in Angular 2 programmatically?

In my Angular 2 application, I am facing an issue with the navigation flow between the home component and the nav panel component. When a user clicks on the logout button in the nav panel, the current URL is correctly shown as "/login" in the console log. ...

Typed NextJs navigation to a specific route

<Link href="/about"> <a>About Us</a> </Link> Is there a way to ensure type safety with NextJs links? Currently, it is challenging to restructure the Link component as it is just a string. I stumbled upon this repos ...

Having trouble retrieving an Enum within an Angular template?

I am trying to use an enum to read a property of an array. However, I encountered an error with the following code: <ng-container *ngFor="let elem of list"> <div class="ui-g-12 ui-sm-12 ui-md-12 ui-lg-12 ui-xl-12"> &l ...

Encountering a problem while trying to run npm publish with public access, error code E

I've encountered an issue when attempting to publish a scoped package on npm, where I consistently receive this error via the CLI: npm ERR! code E403 npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/@username%2fdynamic-ui-elements - Forbidd ...

AngularJS Mapping between two JSON files

What is the most effective way to map data between 2 JSON files in AngularJS? I need to connect and display information from these files in a table. JSON File 1 [{ "year": 2013, "doctor": "Dr. Smith", "illness": "Flu", "apptdate": " ...

Guide to accessing the request object within the entity class in NestJS

Currently tackling a project in nestJs that requires adding audit columns to certain entities. I have set up user information on a request object, which seems to be working fine when printed in the controller. However, I am facing issues with implementing ...

Using Laravel 5.2 passport for secure authentication with Angular 2

Being new to Laravel Passport, I find it quite confusing. I've watched Taylor Otwell's tutorial on Passport but still can't figure out if it's possible to authenticate an Angular app with Laravel Passport. My goal is to develop a Full ...

After reinstalling all components, the system does not recognize 'ng'

After encountering numerous issues with the latest version of node.js, I decided to start fresh by uninstalling everything. I removed the angular/CLI and uninstalled npm using the command npm uninstall -g npm. I then deleted the npm file and uninstalled no ...

Generating charts using JSON data with the help of JavaScript

Hi there! I have a JSON file shown in the first image and I would like to create a chart similar to the one in the second image using JavaScript (using any library). Can anyone provide me with a simple example code on how to achieve this? ...

Obtaining demographic information from a JSON object

I'm currently facing an issue while trying to import the insights data as a json object from Facebook. Some of the vital data such as "Organic shares" and "Demographics" are missing in the JSON object generated. Is there any effective method or soluti ...

Error TS2403: All variable declarations following the initial declaration must be of the same type in a React project

While developing my application using Reactjs, I encountered an error upon running it. The error message states: Subsequent variable declarations must have the same type. Variable 'WebGL2RenderingContext' must be of type '{ new (): WebGL2 ...

Having trouble with json_decode function not functioning correctly

After successfully decoding the commented "$jsonString" String, I encountered an issue when trying to decode it using curl. The result showed Null. I could really use some assistance with this problem. if (isset($_POST['dkno'])) { $dcktNo = $ ...

Exploring the redirection behavior of Angular authentication guards

My implementation involves an Angular authenticated guard. @Injectable({ providedIn: 'root' }) export class AuthenticatedGuard implements CanActivate, CanActivateChild { constructor(@Inject('Window') private window: Window, ...

JSON.net in C# facing issues when deserializing dictionaries with objects as values

I'm facing an issue with my "Settings" class and the serialization/deserialization process using JSON.NET. While it correctly writes the settings data into a .json file, the objects are not being created correctly upon reading the file. I attempted t ...

Encountering an error code TS5055 when attempting to call an API within a TypeScript constructor

I'm attempting to retrieve a list of customers by calling a GET API from a dashboard constructor as shown below: // tslint:disable-next-line:max-line-length constructor(public addCustomerDialog: MatDialog, private router: Router, private rout ...

initiate an animated sequence upon the initialization of the Angular server

Is there a way to launch a Netflix animation after my server has started without success using setTimeout? I don't want to share the lengthy HTML and CSS code. You can view the code for the animation in question by visiting: https://codepen.io/claudi ...

Error encountered: Unable to convert Array data type to a byte array

Trying to convert Json data to a byte array and then store it in an SQL image field, here is the code snippet: public string Post([FromBody] dynamic data) { foreach (var item in data) { imgPhoto1 = (byte[])item["Photo1&q ...