What could be causing the issue with my output not displaying correctly?

Hey guys! I'm working on creating a to-do list but I've encountered a problem. Whenever I enter a value in the text field, it doesn't get added to the array of list elements. Strangely enough, when I console.log it, it seems to work. Can anyone spot what I might be missing? Here is the code snippet:

 <div class="row">
    <div class="col-xs-12">
      <input type='text' name='unos' #unos placeholder="Enter task" /> 
      <button class="btn btn-primary" (click)="addTask(unos.value)">Add</button>

    <ul class="list-group">
      <li class="list-group-item" *ngFor="let el of tasks">
       <span>{{ el.content }}</span> <span><input type='checkbox'></span>
         <button class="btn btn-danger" (click)="removeTask(el)">Remove</button>
      </li>      
    </ul>
  </div>
</div>

import { Component, OnInit } from '@angular/core';

import { Task } from '../task.model';

@Component({
  selector: 'app-task-list',
  templateUrl: './task-list.component.html',
  styleUrls: ['./task-list.component.css']
})
export class TaskListComponent implements OnInit {

  tasks: Task[] = [
    {content: 'Buy milk'},
    {content: 'Go for a run'},
    {content: 'Eat pizza'}
  ];

  removeTask(task: Task){
    this.tasks.splice(this.tasks.indexOf(task), 1);
  }

  addTask(task: Task) {
    this.tasks.push(task);
    console.log(task);
  }

  constructor() { }

  ngOnInit() {
  }

}

Answer №1

There seems to be an issue with the current setup.

<button class="btn btn-primary" (click)="dodaj(unos.value)">dodaj</button>

When this code is executed, it will return a string as output instead of an element type.

To fix this, you need to take the input as a string, create an element, and then push it into the array like shown below:

dodaj(item: string) {
    const element : Element = {sadrzaj: item};
    this.elementi.push(element );
    console.log(item);
  }

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

Tips on how to effectively simulate a custom asynchronous React hook that incorporates the useRef() function in jest and react-testing-library for retrieving result.current in a Typescript

I am looking for guidance on testing a custom hook that includes a reference and how to effectively mock the useRef() function. Can anyone provide insight on this? const useCustomHook = ( ref: () => React.RefObject<Iref> ): { initializedRef: ...

Discovering the country associated with a country code using ngx-intl-tel-input

In my application, I am trying to implement a phone number field using this StackBlitz link. However, I have observed that it is not possible to search for a country by typing the country code (e.g., +231) in the country search dropdown. When I type a coun ...

Is including takeUntil in every pipe really necessary?

I'm curious whether it's better to use takeUntil in each pipe or just once for the entire process? search = (text$: Observable<string>) => text$.pipe( debounceTime(200), distinctUntilChanged(), filter((term) => term.length >= ...

Angular application using ngrx-store-localstorage does not retain data after a page refresh

Looking to incorporate ngrx into my authentication flow with the help of ngrx-store-localstorage for token persistence between browser sessions. After logging in, I can see the token value stored like this: {"token":{"token":"e5cb6515-149c-44df-88d1-4ff1 ...

The error at core.js line 4442: TypeError - Unable to access the 'trim' property of an undefined value

I encountered an issue with the 'trim' function in my Angular Node MySQL CRUD Application core.js:4442 ERROR TypeError: Cannot read property 'trim' of undefined at AdmissionListComponent.post (admission-list.component.ts:30) at Admissi ...

"Creating a backend server using Node.js, TypeScript, and g

I am currently in the process of developing a nodejs project that will consist of 3 key services: Gateway Product Order The Product and Order services will perform functions related to their respective names, while the Gateway service will take JSON requ ...

Executing a series of imported functions from a TypeScript module

I'm developing a program that requires importing multiple functions from a separate file and executing them all to add their return values to an expected output or data transformation. It's part of a larger project where I need these functions to ...

The application of Angular's extension may experience functionality issues due to a failed ngcc operation

https://i.stack.imgur.com/Q7eFX.png The current issue I am encountering. https://i.stack.imgur.com/Kj1r0.png However, the command ng serve is functioning smoothly without any errors. https://i.stack.imgur.com/0SluL.png I'm also facing this partic ...

How can I show the initial three digits and last three digits when using ngFor loop in Angular?

Greetings! I have a list of numbers as shown below: array = [1,2,3,4,5,6,7,8,9,10] By using *ngFor, I am displaying the numbers like this: <div *ngFor =" let data of array"> <p>{{data}}</p> </div> Now, instead of d ...

sass: node_modules/ionic-angular/themes/ionic.variables.scss

Encountered error during the execution of Ionic Cordova build command sass: node_modules/ionic-angular/themes/ionic.functions.scss Full Error: [11:34:50] sass started ... [11:34:51] sass: node_modules/ionic-angular/themes/ionic.functions.scss, line: 35 ...

Syntax for Angular services

I need help figuring out how to send specific data to the backend of my node.js and mysql application in order to trigger an email notification. Specifically, I want to pass two objects (account and labSwap) using a post method. What is the correct syntax ...

Sending an email using Angular is a straightforward process that involves utilizing the built

I need help figuring out how to code a function in Angular or TypeScript that will open Gmail when a specific email address is clicked. I've tried different methods but haven't been successful so far. ...

Issue with Parcel / React 18 App.js failing to refresh cache

Currently, I am developing a React application for my school project. However, I have encountered an issue where certain components are not rendering in my App.js file. Strangely, when I place these components as child components of App.js, they do render ...

Ways to determine if the promise catch block utilizes this.error

I am currently facing an issue: My goal is to test if a specific error message is assigned to this.loginError after the promise fails. However, it appears that the test is failing because the promise must eventually resolve. I have attempted various meth ...

Adjusting Sass variable dynamically in Ionic 3 component using Angular 5

Is there a way to dynamically change the primary color on one page and have it apply throughout the entire app? I came across a tutorial that explains how to achieve this: . I am trying to adapt it to my own project by following these steps: In my app.ht ...

Encountering an error message indicating module '@schematics/angular/utility/json-file' cannot be found while attempting to set up Angular universal

Encountering this issue while attempting to install Angular Universal by running the command add @nguniversal/express-engine --clientProject [project name]. My current Angular version is 7. I attempted to resolve it by executing npm install @schematics/&l ...

Having issues with Angular 16: The module 'SharedModule' is importing the unexpected value 'TranslationModule'. Remember to include an @NgModule annotation

Upon upgrading the angular version, I ran into this issue. The current version utilizing angular-l10n is v8.1.2 and my TypeScript version is v4.9.5. import { TranslationModule } from 'angular-l10n'; @NgModule({ imports: [ CommonModul ...

"Unlocking the full potential of Typescript and Redux: Streamlining the use of 'connect' without the need to

I am facing some challenges with typescript and redux-thunk actions. The issue arises when my components heavily rely on react-redux connect to bind action creators. The problem occurs when I create the interface for these redux actions within the compone ...

Navigating the use of a getter property key within a generic method signature

What I want to do is create a class with descendants that have a method signature that can adapt based on a compile-time fixed property, which can also be overridden. Here's an example: class Parent { public get config() { return { foo: & ...

The value of Angular Input remains unchanged within a FormArray

I am experiencing an issue with the Sequence No in my PreprocessingForm's FormArray. When I add a new row, the Sequence No does not change as expected. <tr class="mat-row" *ngFor="let dynamic of PreprocessingForm.controls.arithmeticI ...