Angular Ionic 3 how to know if an image is loaded and if there are any errors on fetching the image url path
I have an Ionic 3 App where I am loading some image from an API server. In the client-side I want to know if the image is fetched or loaded successfully and I also want to know if there are any errors on fetching.
Here is the sample that I created using pure html with angular template without any js.
<img hidden [src]="driverInfo?.user_avatar?.url | image:'original'" (load)="this.loadingImage = false">
<img [src]="driverInfo?.user_avatar?.url | image:'original'" *ngIf="!loadingImage" onError="this.src='assets/imgs/avatar-placeholder.png';" />
When I only use the second line of code below and transfer the (load) event there. The onError
path shows first which is not because the image is still loading. I want to show a spinner or loader when the image is loading. Then when the image has loaded show the original path if it succeed if not show the path in the onError.
So I know this code is not good at all. That's why I am trying to solve here. I just created a workaround code that achieves my goal but not in a good practice.
So in the first line of the code. I displayed a hidden html file. The purpose of this code is only to trigger if the image is loaded and in the second line is where the actual image is displayed if it is fetched successfully or show some error or image placeholder if not.
So how can I refactor this using Angular JS? Will I use ViewChild with ElementRef to achieve this?
Appreciate if someone could help. Thanks in advance.
android angular image ionic-framework ionic3
add a comment |
I have an Ionic 3 App where I am loading some image from an API server. In the client-side I want to know if the image is fetched or loaded successfully and I also want to know if there are any errors on fetching.
Here is the sample that I created using pure html with angular template without any js.
<img hidden [src]="driverInfo?.user_avatar?.url | image:'original'" (load)="this.loadingImage = false">
<img [src]="driverInfo?.user_avatar?.url | image:'original'" *ngIf="!loadingImage" onError="this.src='assets/imgs/avatar-placeholder.png';" />
When I only use the second line of code below and transfer the (load) event there. The onError
path shows first which is not because the image is still loading. I want to show a spinner or loader when the image is loading. Then when the image has loaded show the original path if it succeed if not show the path in the onError.
So I know this code is not good at all. That's why I am trying to solve here. I just created a workaround code that achieves my goal but not in a good practice.
So in the first line of the code. I displayed a hidden html file. The purpose of this code is only to trigger if the image is loaded and in the second line is where the actual image is displayed if it is fetched successfully or show some error or image placeholder if not.
So how can I refactor this using Angular JS? Will I use ViewChild with ElementRef to achieve this?
Appreciate if someone could help. Thanks in advance.
android angular image ionic-framework ionic3
The best would be to usedirective
since you may need this functionality in multiple places.
– Sunil Singh
Nov 16 '18 at 21:59
add a comment |
I have an Ionic 3 App where I am loading some image from an API server. In the client-side I want to know if the image is fetched or loaded successfully and I also want to know if there are any errors on fetching.
Here is the sample that I created using pure html with angular template without any js.
<img hidden [src]="driverInfo?.user_avatar?.url | image:'original'" (load)="this.loadingImage = false">
<img [src]="driverInfo?.user_avatar?.url | image:'original'" *ngIf="!loadingImage" onError="this.src='assets/imgs/avatar-placeholder.png';" />
When I only use the second line of code below and transfer the (load) event there. The onError
path shows first which is not because the image is still loading. I want to show a spinner or loader when the image is loading. Then when the image has loaded show the original path if it succeed if not show the path in the onError.
So I know this code is not good at all. That's why I am trying to solve here. I just created a workaround code that achieves my goal but not in a good practice.
So in the first line of the code. I displayed a hidden html file. The purpose of this code is only to trigger if the image is loaded and in the second line is where the actual image is displayed if it is fetched successfully or show some error or image placeholder if not.
So how can I refactor this using Angular JS? Will I use ViewChild with ElementRef to achieve this?
Appreciate if someone could help. Thanks in advance.
android angular image ionic-framework ionic3
I have an Ionic 3 App where I am loading some image from an API server. In the client-side I want to know if the image is fetched or loaded successfully and I also want to know if there are any errors on fetching.
Here is the sample that I created using pure html with angular template without any js.
<img hidden [src]="driverInfo?.user_avatar?.url | image:'original'" (load)="this.loadingImage = false">
<img [src]="driverInfo?.user_avatar?.url | image:'original'" *ngIf="!loadingImage" onError="this.src='assets/imgs/avatar-placeholder.png';" />
When I only use the second line of code below and transfer the (load) event there. The onError
path shows first which is not because the image is still loading. I want to show a spinner or loader when the image is loading. Then when the image has loaded show the original path if it succeed if not show the path in the onError.
So I know this code is not good at all. That's why I am trying to solve here. I just created a workaround code that achieves my goal but not in a good practice.
So in the first line of the code. I displayed a hidden html file. The purpose of this code is only to trigger if the image is loaded and in the second line is where the actual image is displayed if it is fetched successfully or show some error or image placeholder if not.
So how can I refactor this using Angular JS? Will I use ViewChild with ElementRef to achieve this?
Appreciate if someone could help. Thanks in advance.
android angular image ionic-framework ionic3
android angular image ionic-framework ionic3
edited Nov 16 '18 at 1:05
KnowledgeSeeker
asked Nov 16 '18 at 1:00
KnowledgeSeekerKnowledgeSeeker
298120
298120
The best would be to usedirective
since you may need this functionality in multiple places.
– Sunil Singh
Nov 16 '18 at 21:59
add a comment |
The best would be to usedirective
since you may need this functionality in multiple places.
– Sunil Singh
Nov 16 '18 at 21:59
The best would be to use
directive
since you may need this functionality in multiple places.– Sunil Singh
Nov 16 '18 at 21:59
The best would be to use
directive
since you may need this functionality in multiple places.– Sunil Singh
Nov 16 '18 at 21:59
add a comment |
1 Answer
1
active
oldest
votes
I think you can achieve something like what you want using the following construction:
<img #thisImage src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" failedUrl="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" (error)="missingImage(thisImage.attributes.failedUrl.value)" onerror="this.src='https://via.placeholder.com/150'"/>
Here is stackblitz to play around: https://stackblitz.com/edit/ionic-szz3nc
In short:
you want to assign additional attribute that will duplicate original value of 'src', I used 'failedUrl' as such. This is because on error src will get replaced with your placeholder safe url.
Now onerror - you want to replace failed url with placeholder
using (error) binding you can call a method and pass whatever you need to a ts function to act further (like I used to "report" such missing images).
Let me know if this is helpful.
Please note this type of code is super risky as if you make a mistake this can cause error loop (like if your safe placeholder url is also pointing nowhere).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329984%2fangular-ionic-3-how-to-know-if-an-image-is-loaded-and-if-there-are-any-errors-on%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you can achieve something like what you want using the following construction:
<img #thisImage src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" failedUrl="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" (error)="missingImage(thisImage.attributes.failedUrl.value)" onerror="this.src='https://via.placeholder.com/150'"/>
Here is stackblitz to play around: https://stackblitz.com/edit/ionic-szz3nc
In short:
you want to assign additional attribute that will duplicate original value of 'src', I used 'failedUrl' as such. This is because on error src will get replaced with your placeholder safe url.
Now onerror - you want to replace failed url with placeholder
using (error) binding you can call a method and pass whatever you need to a ts function to act further (like I used to "report" such missing images).
Let me know if this is helpful.
Please note this type of code is super risky as if you make a mistake this can cause error loop (like if your safe placeholder url is also pointing nowhere).
add a comment |
I think you can achieve something like what you want using the following construction:
<img #thisImage src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" failedUrl="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" (error)="missingImage(thisImage.attributes.failedUrl.value)" onerror="this.src='https://via.placeholder.com/150'"/>
Here is stackblitz to play around: https://stackblitz.com/edit/ionic-szz3nc
In short:
you want to assign additional attribute that will duplicate original value of 'src', I used 'failedUrl' as such. This is because on error src will get replaced with your placeholder safe url.
Now onerror - you want to replace failed url with placeholder
using (error) binding you can call a method and pass whatever you need to a ts function to act further (like I used to "report" such missing images).
Let me know if this is helpful.
Please note this type of code is super risky as if you make a mistake this can cause error loop (like if your safe placeholder url is also pointing nowhere).
add a comment |
I think you can achieve something like what you want using the following construction:
<img #thisImage src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" failedUrl="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" (error)="missingImage(thisImage.attributes.failedUrl.value)" onerror="this.src='https://via.placeholder.com/150'"/>
Here is stackblitz to play around: https://stackblitz.com/edit/ionic-szz3nc
In short:
you want to assign additional attribute that will duplicate original value of 'src', I used 'failedUrl' as such. This is because on error src will get replaced with your placeholder safe url.
Now onerror - you want to replace failed url with placeholder
using (error) binding you can call a method and pass whatever you need to a ts function to act further (like I used to "report" such missing images).
Let me know if this is helpful.
Please note this type of code is super risky as if you make a mistake this can cause error loop (like if your safe placeholder url is also pointing nowhere).
I think you can achieve something like what you want using the following construction:
<img #thisImage src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" failedUrl="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" (error)="missingImage(thisImage.attributes.failedUrl.value)" onerror="this.src='https://via.placeholder.com/150'"/>
Here is stackblitz to play around: https://stackblitz.com/edit/ionic-szz3nc
In short:
you want to assign additional attribute that will duplicate original value of 'src', I used 'failedUrl' as such. This is because on error src will get replaced with your placeholder safe url.
Now onerror - you want to replace failed url with placeholder
using (error) binding you can call a method and pass whatever you need to a ts function to act further (like I used to "report" such missing images).
Let me know if this is helpful.
Please note this type of code is super risky as if you make a mistake this can cause error loop (like if your safe placeholder url is also pointing nowhere).
answered Nov 16 '18 at 5:16
Sergey RudenkoSergey Rudenko
2,8932822
2,8932822
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329984%2fangular-ionic-3-how-to-know-if-an-image-is-loaded-and-if-there-are-any-errors-on%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The best would be to use
directive
since you may need this functionality in multiple places.– Sunil Singh
Nov 16 '18 at 21:59