How can we update a boolean value in an Angular service using a set function?

Hey there! I'm currently working on updating a boolean value in my service when a button is clicked within my component. My goal is to trigger the setfunction and toggle the boolean value from true to false, and vice versa when the button is clicked again.

Service

import { Injectable } from "@angular/core";

import { Melding } from "./melding";
import { Subject } from "rxjs";

@Injectable({
    providedIn: "root"
})
export class MeldingService {
    checkedChange: Subject<Boolean> = new Subject<Boolean>();
    private meldingen = new Array<Melding>(
        { id: 1, name: "Zinc verstopt Machine A", checked: false, roleId: 1 , beschrijving: "Ga de machine maken. dit is de beschrijving lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum "},
        { id: 2, name: "Storing Machine A", checked: false, roleId: 1 , beschrijving: "Ga de machine maken. dit is de beschrijving"},
        { id: 3, name: "Zinc verstopt Machine B", checked: false, roleId: 2 , beschrijving: "Ga de machine maken. dit is de beschrijving"},
        { id: 4, name: "Storing Machine B", checked: false, roleId: 2 , beschrijving: "Ga de machine maken. dit is de beschrijving"},
        { id: 5, name: "Defect Machine B", checked: false, roleId: 2 , beschrijving: "Ga de machine maken. dit is de beschrijving"},
        { id: 6, name: "Zinc verstopt Machine C", checked: false, roleId: 3 , beschrijving: "Ga de machine maken. dit is de beschrijving"},
        { id: 7, name: "Zinc verstopt Machine D", checked: false, roleId: 4 , beschrijving: "Ga de machine maken. dit is de beschrijving"},
        { id: 8, name: "Zinc verstopt Machine E", checked: false, roleId: 5 , beschrijving: "Ga de machine maken. dit is de beschrijving"},
        { id: 9, name: "Storing Machine E", checked: false, roleId: 6 , beschrijving: "Ga de machine maken. dit is de beschrijving"}
    );

    setChecked(id: number) {
        var melding = this.getMelding(id);
        console.log(!melding.checked);
        this.checkedChange.next(!melding.checked);

    }
}

Component

import { ActivatedRoute } from "@angular/router";

import {Melding} from "../melding/melding";
import {MeldingService} from "../melding/melding.service";

@Component({
  selector: 'ns-melding-detail',
  templateUrl: './melding-detail.component.html'
})
export class MeldingDetailComponent implements OnInit {
  melding: Melding;

  constructor(
    private meldingService: MeldingService, 
    private route: ActivatedRoute) { }

  ngOnInit(): void {
    const id = +this.route.snapshot.params.id;
    this.melding = this.meldingService.getMelding(id);
  }

  onButtonClick(id: number) { 
    this.meldingService.setChecked(id);
  } 
}

Answer №1

Can you clarify your question? Also, could you explain the function of getMelding without using a mix of Dutch and English?

In your implementation of the setChecked method, shouldn't you be toggling the value of meldingen[id].checked instead?

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

Alter the entity type when passing it as a parameter

Trying to alter the Entity type, I am invoking a function that requires two parameters - one being the entity and the other a location. However, when trying to assign a Type for the first entity, it throws an error: Error: Argument of type 'Node<En ...

Setting up Firebase in an NX Workspace

Looking for a way to set up an Nx Workspace with Firebase that can deploy multiple applications within the monorepo. Ideal file structure would resemble something like this: https://i.sstatic.net/oKJrT.png To deploy the various applications, hoping to pas ...

Issue with Dropdown menu in Navbar on Bootstrap 5 when using ng add installation technique

Here's my current setup: I'm currently using Angular 11.2.11 and Bootstrap 5 on a Windows system. The project I'm working on utilizes SCSS. The Issue at Hand: I've been following the codes provided in the bootstrap documentation (whi ...

What is the process for implementing mandatory field validation on a group of checkboxes?

Looking for assistance with Angular 2 Checkbox Selection: Check out the updated code snippet below: HTML <b><label *ngFor="let type of topics;let i=index" class="checkbox-inline"> <input type="checkbox" id="{{'ch ...

What is the best way to convert Observable<Observable<{...}>[ ]> to Observable<{...}[ ]>?

I am currently working on merging two observable objects into a single observable to access data from both. These observables represent documents from separate collections in a NoSQL database, specifically a Cloud Firestore database. Both collections have ...

The bottom of the page displays varying outcomes between Code Snippet (CodePen) and Angular

I had the idea of having my footer cover the entire length of the page and stick to the bottom, adjusting itself if more content is added. However, I encountered an issue where on Codepen everything works perfectly, but on Angular (14), the footer does not ...

Developing new Azure AD users through Angular using MSAL.js

Is it feasible, from the perspective of Web Applications, for a user to create an Azure AD account by inputting their email, name, phone number, and other details that can then be forwarded to Azure AD through the web application? I am currently exploring ...

Angular4 nested components within a Bootstrap table offer a dynamic and visually appealing way

As a beginner with Angular 4, I am working on creating a bootstrap table with nested components. The child component is supposed to display in a single row, but the table is not rendering correctly. Please refer to the image below for a snapshot: snapshot ...

What are the benefits of reverting back to an Angular 1 directive instead of using an Angular 2 application

Our team has created numerous Angular 1 components and is eager to incorporate them into our Angular 2 application. However, the documentation suggests that we must downgrade the Angular 2 application in order to integrate and utilize the Angular 1 direc ...

Creating a custom currency input directive for ion-input: A step-by-step guide

Update: Initially, I believed the issue lied within the implementation of the ControlValueAccessor, but later discovered that the problem was related to applying the ControlValueAccessor to child elements. The question has been edited to reflect this. I a ...

The TypeScript error message "Type 'x' cannot be assigned to type 'IntrinsicAttributes & IntrinsicClassAttributes<Class>'" was displayed on the screen

I encountered an issue when trying to pass a class method to a child component in a new code. While it worked perfectly in another code, I am now receiving the following error message: view image here What steps should I take to resolve this error? Do I ...

How can Angular send datetime data to Nodejs in the most effective manner?

Working with the primeng calendar component within a template-driven form, I encountered an issue. When passing the date 16/05/2018 11:45 from Angular to Node, it gets converted to 2018-05-16T06:15:33.000Z. I discovered that I could convert it back to IST ...

Tips for retrieving an object from an array with Angular and Firestore

Currently, I am attempting to retrieve an object from Firestore using the uid so that I can return a specific object as a value. I have implemented a function in order to obtain the object 'Banana'. getFruit(fruitUid: string, basketUid: string) ...

Angular Version 11 is throwing a NullInjectorError with the message: "No provider found for Control

I recently implemented a custom input component based on the guidance provided in this interesting article: medium.com: dont-reinvent-the-wheel. Below is the code snippet I used, written in strict mode ▼ // input.component.ts import { Component, Input, ...

Mobile Devices and Local Storage: What You Need to Know for Safe and Efficient Use. Looking for advice from experienced developers

Need help with caching user input on an Angular11 + Firebase app? Let's discuss implementing a caching feature for a dynamic form section that can contain varying fields based on the use case. The goal is to save user input in LocalStorage to ensure ...

Tips for effectively sending prop to a component in React with the help of TypeScript

Hey there, I'm working on a component called FormField which can accept either an icon for create or edit. Currently, I am using this FormField inside another component called SelectWithFormField. Here's how it looks: const FormField = ({create, ...

Troubleshooting issues with importing modules in TypeScript when implementing Redux reducers

Struggling to incorporate Redux with TypeScript and persist state data in local storage. My current code isn't saving the state properly, and as I am still new to TypeScript, I could really use some suggestions from experienced developers. Reducers i ...

Facing challenges with parsing JSON files in an Angular 2+ application

Utilizing the Angular CLI, I have configured this project with the standard folder layout. My goal is to practice reading a JSON file from src/app/assets/items.json and then using it to display these items in the HTML. items.json: { "results": [ ...

The paths configuration in tsconfig.json is not functioning as anticipated

I've been encountering a module not found error while trying to work with jasmine-ts. To troubleshoot, I decided to use tsc and inspect my folder structure: \src\app\... \src\tests\... To address this issue, I created a ...

Storing various checkbox values into a database

I have a form with checkboxes that I need to submit to an API for database storage. This is the city.page.html <ion-item *ngFor="let city of cities;"> <ion-checkbox [(ngModel)]="city.id" ></ion-checkbox> <i ...