4

I'm new to angular. I couldn't figure how to access a variable inside a function(). this is my code

  mergeImages() {
    var imgurl;
    var canvas: HTMLCanvasElement = this.canvas.nativeElement;
    var context = canvas.getContext('2d');


    let img1 = new Image();
    let img2 = new Image();

    img1.onload = function() {
      canvas.width = img1.width;
      canvas.height = img1.height;
      context.globalAlpha = 1.0;
      img2.src = '../assets/sun.jpg';
    };
    img2.onload = function() {
      context.globalAlpha = 1;
      context.drawImage(img1, 0, 0);
      context.globalAlpha = 0.5; //Remove if pngs have alpha
      context.drawImage(img2, 0, 0);

       imgurl = canvas.toDataURL("image/jpg");
      //console.log(imgurl)

    };
    img1.src = '../assets/moon.jpg';


     }

now I need to access "imgurl" from another method

 printvalue(){
   need to access imgurl
}

edit 1 - problem is angular cannot find var a on printvalue() it's only working inside function something()

4
  • did you use typescript Commented Mar 16, 2018 at 5:43
  • yes I'm using typescript Commented Mar 16, 2018 at 5:43
  • local vs global variable ? i think it not angular/typescript issue. Commented Mar 16, 2018 at 6:02
  • please see my updated question Commented Mar 16, 2018 at 6:13

2 Answers 2

3

here you want to make a scope variable and access throughout the component, earlier in angularJS, there was $scope variable where you could access that variable throughout, in the latest angular version you need to use this to access throughout.

so you need to try

     imgurl : string;
mergeImages() {

    var canvas: HTMLCanvasElement = this.canvas.nativeElement;
    var context = canvas.getContext('2d');


    let img1 = new Image();
    let img2 = new Image();

    img1.onload = () => {
      canvas.width = img1.width;
      canvas.height = img1.height;
      context.globalAlpha = 1.0;
      img2.src = '../assets/sun.jpg';
    };
    img2.onload = () => {
      context.globalAlpha = 1;
      context.drawImage(img1, 0, 0);
      context.globalAlpha = 0.5; //Remove if pngs have alpha
      context.drawImage(img2, 0, 0);

       this.imgurl = canvas.toDataURL("image/jpg");
      //console.log(imgurl)

    };
    this.img1.src = '../assets/moon.jpg';


     }
Sign up to request clarification or add additional context in comments.

7 Comments

problem is angular cannot find var a on printvalue() it's only working inside function something()
while assigning the value to a use this.a = 20 @StephenReynolds
@StephenReynolds also you are declaring a variable in dosomething function you need to declare that globally. check my updated answer
Actually already tried all these methods but no success I'll delete this question and repost it with original code. anyway thanx for taking your valuable time to answer my question :)
ok stackoverflow don't allow me to delete this. so I'll edit this question
|
1

Convert your code like this

let imgurl;
mergeImages() {

    var canvas: HTMLCanvasElement = this.canvas.nativeElement;
    var context = canvas.getContext('2d');


    let img1 = new Image();
    let img2 = new Image();

    img1.onload = () => {
      canvas.width = img1.width;
      canvas.height = img1.height;
      context.globalAlpha = 1.0;
      img2.src = '../assets/sun.jpg';
    };
    img2.onload = () => {
      context.globalAlpha = 1;
      context.drawImage(img1, 0, 0);
      context.globalAlpha = 0.5; //Remove if pngs have alpha
      context.drawImage(img2, 0, 0);

       this.imgurl = canvas.toDataURL("image/jpg");
      //console.log(imgurl)

    };
    this.img1.src = '../assets/moon.jpg';


     }

4 Comments

problem is angular cannot find var a on printvalue() it's only working inside function something()
@ anas first of all thanx for taking your valuable time to answer my question. when I tried to declare letimgurl; my ide gives error under "let" saying "a constructor method or accseor or property was expected "
@StephenReynolds Have you used function keyword ? It is not supposed to use in angular
yes first I used that but then I changed it as you posted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.