No defined outcome when using a conditional input with a name attribute

I am facing an issue while trying to determine whether to show/hide mybutton. The console keeps throwing an error stating that mybutton is undefined whenever it tries to evaluate mybutton.disabled.

To troubleshoot, I included a true statement: 1===1, but the same error persists. What could be causing this? How can I refactor this code so that I can successfully add a condition?

<div [ngSwitch]="item.id">
    <ng-template [ngSwitchCase]="id1">
        <pv-my-selector (onSaveShow)="onSaveShow(mybutton, $event)"></pv-my-selector>
    </ng-template>
</div>
<button *ngIf="1===1" #mybutton mat-button (click)="saveClick(item.id)">Save</button>

onSaveShow(mybutton: any, show: boolean) {
    mybutton.disabled = !show; <!-- the error seems to originate here -->
}

saveClick(id: string) {

}

Answer №1

Prior to the button block being rendered, the following block is displayed and the mybutton is currently undefined as it has not yet been defined.

<div [ngSwitch]="item.id">
    <ng-template [ngSwitchCase]="id1">
        <pv-my-selector (onSaveShow)="onSaveShow(mybutton, $event)"></pv-my-selector>
    </ng-template>
</div>

I recommend utilizing ViewChild() to import the button into your component:

@ViewChild('mybutton', {static: false}) mybutton;

Ensure that the button exists before making any modifications to it:

onSaveShow(show: boolean) {
    if (this.mybutton) {
      this.mybutton.disabled = !show;
    }
}

An alternative approach would be to utilize [hidden] instead of *ngIf.

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

Refactor TypeScript in Visual Studio Code: Relocate class from one file to another (already existing) file

One common task when reorganizing a project with numerous classes is to transfer certain classes to an already existing file. After exploring various vscode features and extensions, I have not come across any refactoring tool that allows for this specific ...

The challenge of validating in Typescript and utilizing type inference

I am currently facing an issue with a function that resembles the one provided below (I have created a simplified example for discussion purposes): interface Variable { someMethod: () => void } const validateVariable(variable: Variable | undefined) { ...

Finding a resolution to the Angular 6 error proves to be challenging as

Error occurred in style.scss file during compilation: Module build failed: Error: ENOENT: no such file or directory, scandir 'C:\Users\arsalan.akhtar\Documents\KEweb\node_modules\node-sass\vendor' at Object. ...

Errors in Angular 2 like Template parsing issues and the inability to bind to certain elements seem to be quite common

I recently started learning AG2 and it's only been a day. I followed an online tutorial and copied the code exactly, but I encountered an error. directives: [favour_component] // this line of code is highlighted in RED After checking the chrome co ...

utilizing type predictors in brand merging

For hours now, I've been struggling with a small problem that seems to have no solution in sight. I wonder if someone with a sharper mind could offer me some guidance? The method I'm using returns a predicate: this is xxx. This method is then us ...

Error: Angular SSR does not recognize IDBIndex

Attempting to build my Angular application using the command npm run build:ssr. The application built successfully, but when running the command npm run serve:ssr, I encounter the following error: ReferenceError: IDBIndex is not defined Note: Upon invest ...

Sending environmental variable values to an Angular application from a Docker file

I am currently facing a challenge with my Angular application where I am attempting to define the environment variable from an external source, specifically from a docker compose file. For reference, I found an article that addresses this issue: docker-c ...

What is the process for obtaining the feedback from a new StepFunctionsStartExecution operation using AWS CDK?

Task Explanation: Iterate through the children in Step Function 1 Forward each JSON object to Step Function 2 Upon completion of Step Function 2, a response is obtained from an external API Utilize this API response within Step Function 1 Visual Represen ...

Error Encountered: Unable to access property 'cars' as it is undefined in Angular Material Table

I'm encountering an issue with the Angular Material Table (Angular Material Table) After using ng generate @angular/material:material-table --name=car-table to create the default angular table, everything works fine. However, when I attempt to injec ...

Mapping an HTTP object to a model

Currently facing an issue with mapping an http object to a specific model of mine, here is the scenario I am dealing with: MyObjController.ts class MyObjController { constructor ( private myObj:myObjService, private $sco ...

Tips for determining if an HTMLElement has already been created

One issue I'm facing is with a third party component that emits an "onCellEdit" event and passes a cell element as a parameter. My goal is to automatically select the entire text in the input element generated inside this cell when the event occurs. ...

The module '...' is encountering an error with the imported value '...'. To resolve this issue, please include a @NgModule annotation

Implementing Angular. Process: Cloned a straightforward UI library to showcase the Angular package standards: https://github.com/jasonaden/simple-ui-lib Developed a new test application with ng new testApp npm link to the simple-ui-lib/dist Lin ...

The argument labeled as 'Subscription' cannot be assigned to the parameter labeled as 'string' in Angular

I am utilizing my Subscription variables to retrieve the API from configuration settings. public ChannelsAPI=this._configservice.getConfiguration("ChannelAPI").subscribe((result) => console.log(result)); This is the method _Configservice.getC ...

Test for comparing objects partially using Jasmine Array

Is there a specific method in jasmine for checking if an array partially matches another array by comparing objects? Considering that the arrays could potentially contain large amounts of data from test files, is there a way to avoid comparing each indivi ...

Incorporate an Array of Objects into the UseState hook with an initial value

I have encountered an issue with the following error message: "Error: Objects are not valid as a React child (found: object with keys {fzfvhv76576, user }). If you meant to render a collection of children, use an array instead." I have been attem ...

Angular 4: Implementing Subscription with Behavior Subject

I am currently working with Angular 4 and utilizing observables. I have a requirement to share data between components as I am using BehaviorSubject along with observables. Below is the code snippet: import { Subject } from 'rxjs/Subject'; imp ...

Deactivate a variable during operation

I have a unique component called book-smart that has the functionality to display either book-update or book-create based on the availability of a BookDto in my book.service. The update feature can be accessed by clicking a button in a table, while the cre ...

I am on the lookout for an Angular 2 application that utilizes Gulp to generate a distribution folder with the newest @angular frameworks

I'm on the lookout for an Angular2 application that incorporates Gulp to generate a 'dist' folder using the most recent @angular libraries. Although I've come across some online examples, they do not utilize the latest @angular librari ...

Incompatibility issue between metadata versions for Angular 4 and @ng-bootstrap libraries

I've been working with the Angular bootstrap module and encountered an issue. After installing the module using the command npm install --save @ng-bootstrap/ng-bootstrap and importing it into the main app module, I attempted to re-run the application ...

Is there a convenient feature in WebStorm for quickly inserting a lambda function in TypeScript that matches the current argument's signature while coding?

While working with promise chains in TypeScript, I often find myself dealing with a syntax tax that can become cumbersome. It would be great to have a way to automate this process, especially when using WebStorm. My ideal situation would involve having an ...