Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
How do i send an image from one angular component to another and display it

I have created a service with an obervable and im trying to send a dataURL from one component to another, then diplay it as an image.

html for the component i want to send data from

<canvas id="patternFrame" width="600px" height="600px"></canvas> 
<ion-button color="medium" (click)='sendPattern()'>send</ion-button>

Typescript for the component i want to send data from

import {StageColorService} from '../../services/stage-color.service';

  constructor(private stageColorService: StageColorService){}

 ngOnInit(){}

  sendPattern(){
    const canv =document.getElementById('patternFrame') as HTMLCanvasElement;
    this.stageColorService.sendStage(canv.toDataURL()); // use service to send image to color component
  };

html code for the component i want to send data to

<img src="" alt="Image" id="onlyImage" width="400" height="200">

Typescript code for the component i want to send data to

import {StageColorService} from '../../services/stage-color.service';

  constructor(private stageColorService: StageColorService) { }

  ngOnInit() {

    this.stageColorService.stage$
        .subscribe(
          image => {
            console.log('image subscription'+ image);
              document.getElementById('onlyImage').setAttribute('src',  image);
            });
  }

typescript code for my service

import { Injectable } from '@angular/core';
import {Subject} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class StageColorService {

  private stageSource = new Subject<any>();
  stage$ = this.stageSource.asObservable();

  constructor() { }

  sendStage(st: any){
    this.stageSource.next(st);
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Please avoid this document.getElementById('onlyImage').setAttribute('src', image);

instead use a variable to display. Eg:

import {StageColorService} from '../../services/stage-color.service';

public imgSrc:string;

constructor(private stageColorService: StageColorService) { }

ngOnInit() {

this.stageColorService.stage$
    .subscribe(
      image => {
        console.log('image subscription'+ image);
          this.imgSrc = image
        });
}

and in html Use

<img [src]="imgSrc" alt="Image" id="onlyImage" width="400" height="200" />

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!