Using Typescript to verify if an image src has been loaded and trigger a specific function

I am trying to implement a feature on my webpage using the following element:

<img src="https://i.pinimg.com/736x/2c/9d/07/2c9d0704ae49dfde914e2b477bf9279c--stick-figure-profile-pictures.jpg" />

Is there a way to detect when the src has finished loading successfully and then trigger a function?

Answer №1

Check out this example for handling image loading events:

In your HTML file:

<img [src]="imagesource" (load)="doSomething()">
<!--(or)-->
<img src="https://i.pinimg.com/736x/2c/9d/07/2c9d0704ae49dfde914e2b477bf9279c--stick-figure-profile-pictures.jpg" (load)="doSomething()">

In your Typescript file:

imagesource="https://i.pinimg.com/736x/2c/9d/07/2c9d0704ae49dfde914e2b477bf9279c--stick-figure-profile-pictures.jpg";

doSomething(){
   console.log("Loaded");
   //perform additional actions here
}

If you want to learn more about detecting when an image has loaded in an img tag, visit the following link:

Detect when image has loaded in img tag

Answer №2

Try this out:

<img src="https://i.pinimg.com/736x/2c/9d/07/2c9d0704ae49dfde914e2b477bf9279c--stick-figure-profile-pictures.jpg" (load)="myFunction()"/>

Take note of the (load)="myFunction()" at the end (the way the content scrolls when it is code is slightly quirky).

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

Is it possible to retrieve ForeignKey model attributes in Angular by utilizing serialized data passed from Django?

I am currently utilizing Angular 8 and Django 3 for my project. My goal is to design a page dedicated to displaying all of the recipes associated with a specific restaurant. To achieve this, I have established two models within my project - one for the Res ...

Explore the functionality of a TypeScript-created Vue component by testing it with Vue-test-utils

In my attempt to validate props with various data types in a Vue component (built using TypeScript), I utilized the Vue-test-utils package. Despite implementing expect().tobe(), there remains an untested line: DropDownList.vue <template> <v-sel ...

Exploring various queries in Firestore

Does anyone know if there is a way to create a sentence similar to this one: return this.db.collection('places', ref => ref.where("CodPais", "<>", pais)).valueChanges(); I have tried using != and <> but neither seem to be valid. ...

Two lines in ChartJS with distinct fill gradients for each

I am facing an issue with ChartJS version 2.6 in my Ionic 3/Angular 4 app. I have set up a line chart config with 2 datasets, but I am struggling to apply individual fill gradients to each line. What I am aiming for is something like this: https://i.stac ...

Value of type 'string' cannot be assigned to type '{ model: { nodes: []; links: []; }; }'

I am a beginner in TypeScript and I have added types to my project. However, I am encountering an error with one of the types related to the graph: Type 'string' is not assignable to type '{ model: { nodes: []; links: []; }; }'.ts(2322) ...

The data type 'Observable<any>' cannot be assigned to the type 'StoresSummaryResults'. The property 'Data' is not present in the 'Observable<any>' type

As a newcomer to using the Observable with Angular 2, I am facing an issue where my structure is not receiving the results despite being able to validate the response from my REST API. Below is the data class in Typescript that I have: import { RESTResul ...

Struggling with "Content" not being recognized in Typescript PouchDB transpilation errors?

I have been diligently working on an Ionic app for the past three months with no major issues during development or deployment to mobile devices. However, yesterday I encountered a frustrating NPM dependency problem while trying to deploy to mobile. In an ...

Adaptable Style Properties Adjusted by Component Size

Check out this awesome React Native component: const CustomView = (props) => { return ( <View style={{ maxHeight: "100%", width: "100%", aspectRatio: 2, borderWidth: 10, borderCo ...

Convert individual packages within the node_modules directory to ES5 syntax

I am currently working on an Angular 12 project that needs to be compatible with Internet Explorer. Some of the dependencies in my node_modules folder are non es5. As far as I know, tsc does not affect node_modules and starts evaluating from the main opti ...

Utilizing Flux Container.create with Typescript 2.8: A Comprehensive Guide

I need to upgrade my TypeScript version from 2.2 to 2.8 in a React/Flux project. Below is the code snippet defining a store using the container.create utility function: import * as React from 'react'; import { Container } from 'flux/utils& ...

"Encountering a module not found issue while trying to

Attempting to test out 3 node modules locally by updating their source locations in the package.json files. The modules in question are sdk, ng-widget-lib, and frontend. ng-widget-lib relies on sdk, while frontend depends on ng-widget-lib. To locally build ...

Retrieve the :id parameter from the URL as a numerical value in Node.js using Typescript

Is there a way to directly get the :id param from the URL as a number instead of a string in order to easily pass it on to TypeORM for fetching data based on a specific ID? Currently, I am using the following approach where I have to create an additional ...

Angular Material select all checkboxes provide a convenient way to select multiple

Need help with implementing a select all checkbox on Angular Material. The goal is to have the master checkbox show Indeterminate when a specific item is clicked, and then turn to checked once all checkboxes are selected. Currently experiencing some unexpe ...

Align images to the bottom of the gallery only if their heights differ

I'm currently working on an image gallery project and have encountered a problem. When all the image labels are of the same length, they align perfectly. However, if the label is longer than one line, it causes the image to move up instead of creating ...

TypeScript 1.6 warning: Component XXX cannot be instantiated or called as a JSX element

var CommentList = React.createClass({ render: function () { return ( <div className="commentList"> Hello there! I am the CommentList component. </div> ); } }); var ...

What is the best way to incorporate an automatic scroll to the following section on a single page website

My goal is to implement a feature that enables automatic scrolling between sections as the user scrolls up or down. The smooth transition should occur when the user reaches halfway through each section, seamlessly moving to the next screen on the same page ...

Best practice for incorporating types into a Redux-toolkit reducer

As someone who is relatively new to TypeScript, I have a specific goal in mind. I am looking to create an interface where: interface ActionType { fieldName: {any one key from the interface FormStateType listed below such as 'name', 'age&ap ...

Some elements that fit the criteria of 'number | function' are not callable at all

Consider a basic function like this: export const sum = (num?: number) => { const adder = (n: number) => { if (!n) { return num; } num = (num && num + n) || n; return adder; }; return a ...

What could be causing the lack of change detection triggering in nested dynamic components?

I'm encountering an issue with change detection in a nested dynamic component that involves content projection. For some reason, the child component is not being automatically triggered for change detection, necessitating manual intervention for every ...

Issue TS2322 occurs when an element is not compatible with type ReactElement. Mysteriously, this error appears inconsistently within the application

I have successfully resolved the error mentioned, but I am seeking clarification on why it is occurring in this specific instance and not in other parts of my application. Additionally, I am curious as to why a function and a ternary with the same type sig ...