Invoke a method within an *ngIf statement in Angular 5

Suppose I have the following code snippet:

<div *ngIf="item">lorem ipsum</div>

Is there a way to trigger a function if the *ngIf condition is true?

Perhaps something like this...

<div *ngIf="(item) : callFunction() ? ...">lorem ipsum</div>

Any assistance on this matter would be greatly appreciated!

Thank you

Answer №1

One way to go about this in Angular is:

<div *ngIf="name; then func(); else false">;</div>

Alternatively, you can take advantage of how *ngIf evaluates logical expressions and use:

<div *ngIf="name?func():false">;</div>

Answer №2

Consider attempting it this way

<div *ngIf="item ===true?callFunction():'otherStuff'">lorem ipsum</div>

Answer №3

Give this a shot

HTML

<div *ngIf="item; then executeFunction; else noFunction"></div>
<ng-template #executeFunction>
  {{run()}}
</ng-template>
<ng-template #noFunction>
 <!-- do something different -->
</ng-template>

TypeScript

run(){
}

If you have a more efficient solution, please share it

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

Update the display immediately upon a change in the state

In my app.js file, the code looks like this: export const App = () => { const [selectedMeals, setSelectedMeals] = useState<string[]>(["allItems"]); const onCheckHandler = (e: any) => { const checkedValue = e.target.value; if (e.targ ...

Optimizing jQuery UI autocomplete choices through filtering

Currently utilizing the jqueryUI autocomplete feature on my website. Let's say I have the following entries in my database... apple ape abraham aardvark Upon typing "a" in the autocomplete widget, a list appears below the input field displaying the ...

The `chrome.windows` API is not defined for this particular Chrome

I'm currently in the process of developing a Chrome extension that will allow users to effectively manage open tabs within a specific application's website. Here is the current manifest file: { "manifest_version": 2, "name": "AT Tabs", ...

How can we develop a strategy to select products based on specific features while keeping costs minimized?

I've got a collection of products with varying costs and features. Each product offers a unique set of features. I'm in need of an algorithm that, when given the specific features I desire, can recommend the most cost-effective combination of pr ...

Problems encountered with cordova-plugin-facebook4 in Ionic 2

I'm encountering the following error: Error: Metadata version mismatch for module /opt/lampp/htdocs/www/ionicApp/node_modules/@ionic-native/facebook/index.d.ts, found version 4, expect when running the command: ionic cordova run android --prod --rel ...

Prevent inputting values into the text field

Below is the code I wrote to prevent users from entering certain values in a textbox: $(".block-keypress").keypress(function(e){ return false; }); While the code is functioning properly, the backspace key is not blocked. ...

A variable must be defined within a specific block in order to be recognized

In an effort to enhance my code within a passport function, I am looking to pull values from a mongodb Database rather than from an array. The initial functioning code appeared as follows: passport.use( new LocalStrategy( { usernameField: ...

Using PHP and AJAX to send a form

Currently, I am attempting to integrate ajax functionality into my form in order to submit it without refreshing the page. However, I have encountered an issue with the php echo command not functioning as expected. When I remove the ajax code, the form s ...

Is there a way to prevent a link from activating when I click on one of its internal elements?

Within a div nested inside an "a" tag (specifically in Link within next.js), there is another div labeled as "like." When clicking anywhere within the main div, the intention is for it to redirect to the destination specified by the "a" tag. However, if th ...

Silhouettes dancing across handcrafted designs

I have been trying to create a custom geometry using vertices and faces, but I am facing an issue where the geometry does not cast shadows on itself and the faces all have the same color as it rotates. I have attempted various solutions but haven't ha ...

How can I designate the first enum value as 1 in a protobuf data structure?

In order to resolve an issue in the dropdown component of Primeng, the first enum value in protobuf must be set to 0. However, this is causing some trouble. Is there a method to change the first enum value to 1 instead? ...

Updating the parent navigation bar after a successful login in a child component in Angular4

In my Angular4 project, I have set up a login page. Within the parent app.component file, I have included a navigation bar with login and signup buttons. Upon successful login, the login and signup buttons should be hidden, and the username should appear i ...

Is there a way to remove elements from a canvas using jquery?

I implemented a jquery code snippet from this source to add a falling confetti effect on my webpage. You can view the demo page by clicking here. The code works smoothly, and I noticed that I can use the start and stop functions to control the updates. Ho ...

Is there a way to apply the style property only when a component is hovered in Next.js?

I would like the ability to hover over a component and have it display with unique CSS characteristics. For instance, if I hover on item A, I only want item A to stand out from the rest. Currently, I am using this method of changing the element's bac ...

Angular - encountering challenges when implementing the native Web Speech API

My goal is to integrate the native Web Speech API directly without relying on any third-party libraries. I have successfully integrated the API into my service and voice recognition is functioning properly, able to generate text from recognized speech. Ho ...

Node.js server for Cross-Origin Resource Sharing

I'm brand new to Node.js and I'm trying to run some example code, but I keep encountering CORS issues. Server.js var http = require('http'); var io = require('socket.io'); server = http.createServer(function(req, r ...

Storing HTML input data into an external JSON file

I am facing an issue with my input form that includes fields for name, age, email, and password. I am attempting to transfer this data from the HTML form to JavaScript, where it will be converted into a JSON string and stored in an external JSON file for f ...

Angular - creating adaptive HTML container with CSS styling

I am trying to achieve a layout similar to the one shown in the image. A container can have multiple elements starting within it or no elements at all. The elements always have a fixed height, and the container's height is determined by the number of ...

Combine several JSON files into a single file

After spending countless hours searching on Google, I finally gathered the courage to ask my question here. I have several json files. localhost/feed01.json localhost/feed02.json localhost/feed03.json All of the json file structures are similar to this ...

Angular - Enhance ngFor index while filtering

I am currently working with a list that utilizes an *ngFor loop in the template: <li *ngFor="let product of products | filterProducts: selectedFilter; index as productId"> <a [routerLink]="['/product', productId]"> {{produc ...