Angular Ionic 3 how to know if an image is loaded and if there are any errors on fetching the image url path










0















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.










share|improve this question
























  • The best would be to use directive since you may need this functionality in multiple places.

    – Sunil Singh
    Nov 16 '18 at 21:59















0















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.










share|improve this question
























  • The best would be to use directive since you may need this functionality in multiple places.

    – Sunil Singh
    Nov 16 '18 at 21:59













0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 1:05







KnowledgeSeeker

















asked Nov 16 '18 at 1:00









KnowledgeSeekerKnowledgeSeeker

298120




298120












  • 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
















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












1 Answer
1






active

oldest

votes


















0














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:



  1. 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.


  2. Now onerror - you want to replace failed url with placeholder


  3. 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).






share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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:



    1. 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.


    2. Now onerror - you want to replace failed url with placeholder


    3. 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).






    share|improve this answer



























      0














      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:



      1. 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.


      2. Now onerror - you want to replace failed url with placeholder


      3. 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).






      share|improve this answer

























        0












        0








        0







        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:



        1. 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.


        2. Now onerror - you want to replace failed url with placeholder


        3. 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).






        share|improve this answer













        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:



        1. 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.


        2. Now onerror - you want to replace failed url with placeholder


        3. 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).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 5:16









        Sergey RudenkoSergey Rudenko

        2,8932822




        2,8932822





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            Evgeni Malkin