The nz-switch function is malfunctioning as a result of an update that has affected its value

<form [formGroup]="businessFoodHygieneForm">
  <div class="box p-4 d-flex jc-between ai-center">
     <span>
        Food Hygiene Link
     </span>
     <label>
      <nz-switch
      class="switch-middle ms-2"
      formControlName="foodHygieneLink"
      (ngModelChange)="onFoodHygieneChange($event)">
      </nz-switch>
  </label>
 </div>
</form>

ts component:

foodHygieneLink: new FormControl(''),

this.businessFoodHygieneForm.patchValue({
  foodHygieneLink:
    selectedBusiness?.foodHygieneLink,
});

onFoodHygieneChange(status: boolean): void {
    this.foodHygieneLink = status;

    const body = { foodHygieneLink: status ? 'Yes' : 'No' };

    this.bizStore.dispatch(
       businessAction.updateBusiness({
          body: body,
          bid: this.bid,
       })
     );
   }

The functionality using nz-switch was successfully implemented but there is a bug where the switch button does not work correctly when set to "No". It should be in the off position, however it remains on.

Answer №1

It seems that the value of your foodHygieneLink in the observable may be 'Yes' or 'No' instead of a boolean.

As a result, the <nz-switch> will only open when the value is No.

To resolve this issue, convert 'Yes' or 'No' to a boolean before assigning it to the foodHygieneLink FormControl.

this.businessFoodHygieneForm.patchValue({
  foodHygieneLink:
    selectedBusiness?.foodHygieneLink === 'Yes',
});

Check out the demonstration on StackBlitz

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

"Upon subscribing, the object fails to appear on the screen

Why is the subscription object not displaying? Did I make a mistake? this.service.submitGbtForm(formValue) .subscribe((status) => { let a = status; // a = {submitGbtFrom: 'success'} console.log(a, 'SINGLE ...

Encountering issues with package resolution in VS Code while setting up a monorepo with yarn workspaces, Vite, React, and

I recently set up a monorepo using yarn@3 workspaces. Here is the structure of my root package.json: { "name": "hello-yarn-workspaces", "packageManager": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

React - Component not updating after Axios call in separate file

Recently I decided to delve into React while working on some R&D projects. One of my goals was to build an application from scratch as a way to learn and practice with the framework. As I started working on my project, I encountered a rather perplexin ...

Having difficulty updating the value of a FieldArray with setFieldValue in Formik

Thank you for taking the time to read this. I am currently working on creating a form using Formik that involves nesting a FieldArray within another FieldArray. Oddly enough, setFieldValue seems to be functioning correctly as I can log the correct values ...

Using ngModel in multiple mat-select elements in Angular 2/4

I have a mat-select dropdown list that allows multiple selections and I am using NgModel to keep track of the user's selected values. The issue arises when I navigate away from the page and return, the user's selected values are not preserved in ...

JavaScript Library function in Angular Component throwing Type Error: Function is Not Recognized

I created a custom Javascript library to group together essential functions that many Angular Components require. The library called calcs.js includes functions like this: function calculateCosts(object) { do some stuff..... return answer; } To use t ...

Include additional information beyond just the user's name, profile picture, and identification number in the NextAuth session

In my Next.js project, I have successfully integrated next-auth and now have access to a JWT token and session object: export const { signIn, signOut, auth } = NextAuth({ ...authConfig, providers: [ CredentialsProvider({ async authorize(crede ...

Passing and removing array parameters in HTTP requests using Angular

I have an Array of statuses objects. Each status has a name and a boolean set to false by default. These represent checkboxes in a form with filters - when a checkbox is checked, the boolean value is set to true: const filters.statuses = [ { name ...

Implementing a scroll-triggered class addition in Angular

I am currently refreshing my knowledge of Angular. After installing AngularCLI, I am attempting to transition from using jQuery to add a class on scroll. Would it be necessary to utilize [ngClass] in order to check a variable with window location? Right ...

Interacting between two occurrences of the identical Angular component

Within a Razor view, I include an angular component: <my-widget id="myWidget" isfullscreen="false" class="some-class"></my-widget> Upon clicking the 'Popup' button, a popup appears in an iframe and the s ...

The functionality of Silex Middleware Authentication varies depending on whether it is being used in Postman or

As a developer with limited experience in back-end development, I have encountered a real problem despite my best efforts. I am currently using Silex as a simple backend for an Angular 2 App. I have implemented a middleware that checks whether the user ha ...

What is the best method for exporting and importing types in React and Next.js apps?

Is there a way to export and import types from API responses in TypeScript? I have a type called Post that I want to re-use in my project. // pages/users.tsx type Post = { id: number; name: string; username: string; email: string; address: { ...

Exporting variables in Angular's Ahead of Time (AoT) compiler is

I recently attempted to incorporate dynamic configuration into my project, following a guide I found in this insightful post. While everything functions smoothly with the JiT compiler, I encountered the following error when attempting to build using the A ...

Sending form input values using FormData on beforeUploadItem in ng2-file upload

File Upload Component: import { FileUploader } from 'ng2-file-upload/ng2-file-upload'; const URL = 'http://localhost:4200/api/ticketsystem/createticket'; public uploader: FileUploader = new FileUploader({ url: URL, itemAlias: 'f ...

Exploring the functionality of generic components in React Native when using TypeScript

As an illustration, consider export class FlatList<ItemT> extends React.Component<FlatListProps<ItemT>> which incorporates the generic type ItemT. How can I utilize it in a .tsx code? When not parametrized, it appears like this: <Flat ...

Utilizing Typescript with Vue 3's Injection Feature

Using the new Vue 3 Composition API, I have created a "store" for reactive data. const state = reactive<State>({ accessToken: undefined, user: undefined, }); export default { state: readonly(state), } When my app is created, I pass the store ...

Guide on implementing findOne for name validation in Node.js using data from MongoDB

Can someone help solve the issue of name duplication and provide guidance on how to execute the codes in Postman API? The attached codes can be found below: *employeemodel.js enter image description here *employeecontroller.js enter image description her ...

Incorporate SVG files into a TypeScript React application using Webpack

I am trying to incorporate an SVG image into a small React application built with TypeScript and bundled using Webpack. However, I am encountering an issue where the image is not displaying properly (only showing the browser's default image for when n ...

Encountering a TypeError when using Webpack and ts-loader to bundle a third-party library

While everything compiles and bundles successfully, a TypeError is encountered in the browser: "box2dweb_commonjs_1.default is undefined." No errors occur when starting webpack-dev-server and reviewing the bundle at http://localhost:8080/webpack-dev-serv ...

Troubleshooting Karma: Dealing with layout issues in Angular 6 and jQuery

Currently, I am implementing a workaround using JQuery to fix a visual bug in a library that I am working with. Everything was going smoothly until I started preparing for a tested version and encountered an error. Within my component, the code looks like ...