Strange Behavior of ngIf and @Input in Angular 2

Despite console indicating false, NgIf appears to always evaluate as true.

The issue stems from the component's HTML below:

 <product-component-tree itemSku="{{item.itemSku}}" selectable="false" classes="col-md-12 col-xs-12"></product-component-tree>

where selectable is set to false...

export class ProductComponentTree {
@Input() classes: string;
@Input() itemSku: string;
@Input() selectable: boolean;
ngOnInit() {
    if (this.itemSku)
        this.productDetailService.getComponents(this.itemSku, true).subscribe(x => {
            this.treeData = x;
        });
    console.log(this.selectable); //prints false
}
}

The HTML template for the component is as follows:

<div class="{{classes}}" *ngIf="selectable">
    something
    <p-tree [value]="treeData" layout="horizontal" selectionMode="checkbox" [(selection)]="selectedProducts" (onNodeSelect)="nodeSelect($event)"></p-tree>
</div>
<div class="{{classes}}" *ngIf="!selectable">
    else
    <p-tree [value]="treeData" layout="horizontal" [contextMenu]="productTreeCm" selectionMode="single" [(selection)]="selectedProduct"></p-tree>
    <p-contextMenu #productTreeCm [model]="items"></p-contextMenu>
</div>

This setup consistently displays the div with "something" in it!

Objective: Correct functionality to display the "else" div when selectable is false.

Answer №1

By setting the selectable attribute to "false", you are providing a string value to the component.

It's important to note that in JavaScript, "false" is considered a truthy value. (For further clarification, refer to this resource: )

To correctly implement this, follow these steps:

<product-component-tree [itemSku]="item.itemSku" [selectable]="false" ...

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

Struggling to populate a fresh JavaScript array with values submitted from a form

I am currently working on a form to collect Family information that will be used for enrolling in a new benefit plan. My goal is to populate an array with the necessary values for each individual to create the benefit record. While researching, I have only ...

A shortcut for calling functions in Lodash

Looking to execute a series of functions using Lodash? Here's an example: const functions = [ () => console.log('Fn 1'), () => console.log('Fn 2') ]; _(functions).each(fn => fn()); Wondering if there is a Lodash ...

Problem encountered with JavaScript getter on iOS 5

While implementing JavaScript getters in my iPad-optimized website, everything was working perfectly until I updated to iOS 5. Suddenly, the site stopped functioning properly. After thorough investigation, I discovered the root cause of the issue. I have ...

How to retrieve data from a nested ng-repeat loop in AngularJS when the outer loop is a separate object

Is it possible to access the grandparent object of an ng-repeat item in angular? I'm having trouble achieving this. In the example below, I want to display the text of the parent object. This is a simplified version; I am seeking a solution to make ...

The property 'apply' is trying to be accessed but is undefined and cannot be read

Not being an expert in frontend development, I've hit a roadblock on what seems like a simple issue. I'm attempting to load one set of nodes from a JSON file successfully, but when trying to add new nodes from another JSON file, I encounter this ...

Update: "Mui V5 - Eliminate collapse/expand icons in TreeView and reduce TreeItem indentation"

My current project involves Mui V5 and I am looking to customize the TreeView component. Specifically, I need to remove the collapse/expand icons as I want them to be integrated into the TreeItem label component on the left side instead of the right. Add ...

Implementing a Collapse and Expand All feature within an Accordion Component

Hey there! I've been attempting to implement a Collapse All feature on my accordion but am having trouble figuring it out. The resource I've been referencing is this one. I've searched around and noticed that this accordion setup is a bit d ...

Guide on creating a decorator for asynchronous functions that runs exclusively when using `Promise.resolve()`?

This decorator is specifically designed for analytics that triggers an event when a Promise is successfully resolved. class Foo { @LogEvent("success") async bar() { await someAction(); } } After researching online, it seems like I need to a ...

Utilize Vue client to call Google Vision API for image recognition

Despite following the Google Documentation to utilize the Vision API, I am encountering a persistent CORS error: Access to XMLHttpRequest at 'https://vision.googleapis.com/v1/images:annotate?key=MY_KEY_HERE' from origin 'https://localhost:3 ...

Is there a way to include a text file in HTML for referencing or posting on my website?

Managing a website where my colleagues frequently post updates can be time-consuming as I am the only one knowledgeable in html and css. I find myself constantly formatting their posts and creating new pages for them. I am exploring ways to simplify this ...

Activate the CSS on a click event using the onClick method

I am trying to implement a transition that triggers when clicking on a specific div element. Currently, the transition only occurs with the active css class. How can I achieve this effect by simply clicking on the div itself? I am using reactjs and believe ...

What is the best way to retain checkbox states after closing a modal?

I have a modal that includes multiple checkboxes, similar to a filter... When I check a checkbox, close the modal, and reopen it, the checkbox I clicked on should remain checked. (I am unsure how to achieve this :/) If I check a checkbox and then click t ...

Changes to the model cannot be realized unless $scope.$apply is used

Are there alternative methods to achieve the desired model change without utilizing $scope injection in an Angular "controller as" approach within the given setup? The HTML: <div data-ng-controller="Buildings as vm"> <select data-ng-model="vm. ...

Client-Side Isomorphic JS: A Guide to Using HTTP Requests

Seeking advice on data population for isomorphic flux apps. (Using react, alt, iso, and node but principles are transferable) In my flux 'store' (), I need to fetch data from an api: getState() { return { data : makeHttpRequest(url) ...

Converting Angular App's local storage from synchronous to asynchronous with Indexed-db

I am utilizing local storage to retrieve data and if the data is not available in local storage, then I make a server API call, add the data to storage for future use, and return the data. This ensures that next time, the data will be retrieved from storag ...

Create a customized bracelet with a specified number of colors in JavaScript

I encountered an interesting challenge recently. The task was to create a bracelet of length n using a set of specified colors, following certain conditions: No two adjacent beads should have the same color (e.g., R R). A pattern of three consecutive bea ...

What is the reason behind Angular5 making an API call upon pressing F5 in the browser?

As a newcomer to angularjs (using v5), I am currently exploring routing. The routing functionality is in place, but I have noticed that data only loads when the f5 key is pressed. For instance, on my sign-in page, clicking the login button triggers an API ...

Guide to defining the typescript type of a component along with its properties

I am facing an issue with my SampleComponent.tsx file: import React from 'react'; type Props = { text: string }; export const SampleComponent: React.FC<Props> = ({text})=><div>{text}</div>; SampleComponent.variant1 = ({tex ...

Is there a way to modify or add to the response object's methods in Express and Node.js?

When using middleware in Express, the framework passes both a res and a req object. These objects enhance the built-in ones from http.ServerResponse and http.ClientRequest respectively. I am curious about the possibility of overriding or extending methods ...

How can I use Angular's $filter to select a specific property

Is there a convenient method in Angular to utilize the $filter service for retrieving an array containing only a specific property from an array of objects? var contacts = [ { name: 'John', id: 42 }, { name: 'M ...