5

I have created a component with selector 'app-circle'. The HTML of the component contains an SVG for a circle.

What I want is - to use multiple 'app-circle' in my main HTML to draw multiple circles with different attributes (say, color). But I cannot seem to be able to do this. Basically, my intention was to re-use 'app-circle' as multiple objects.

(Please forgive me for being naive; I am new to angular & web development and my experience is mainly in C#, which might be the reason for me to find difficulty in wrapping around angular/web concepts and way of working)

Here is the code: -

main.html

<div class="diagram-wrapper">
  <app-circle></app-circle>
  <app-circle></app-circle>
</div>

circle.component.html

<svg class="range" height="100" width="100">
  <circle class="circle" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>

circle.component.ts

import { Component, OnInit, AfterViewInit } from '@angular/core';

@Component({
            selector: 'app-circle',
            templateUrl: './circle.component.html',
            styleUrls: ['./circle.component.scss']
          })
export class CircleComponent implements OnInit, AfterViewInit {

constructor() { }

ngOnInit() { }

ngAfterViewInit() {
 // Circle logic (fill colors, etc.)
}
5
  • This should work. Could you reproduce your issue on stackblitz.io ? This would allows us to see what you did wrong Commented Jul 25, 2018 at 12:52
  • 2
    You need to use @Input() in order to customize your angular components. If you don't use @Input() all the instances will look the same Commented Jul 25, 2018 at 12:53
  • Read more here Commented Jul 25, 2018 at 12:54
  • @trichetriche Thanks for replying. Here is the link to stackblitz: - stackblitz.com/edit/… This works fine. But both the circles have the same color. What I wanted was to have two (or more) circles with different colors (or other attributes). I was thinking to make it like a UI control which I can re-use in different places without affecting each other. Commented Jul 25, 2018 at 14:28
  • @CristianTraìna Thanks for the suggestion. Will try that. Commented Jul 25, 2018 at 14:30

1 Answer 1

4

Following the suggestion in the comments, I added the @Input to a fork of your stackblitz here: https://stackblitz.com/edit/angular-jppwhz?file=src%2Fapp%2Fapp.component.html

The HTML uses binding to bind the desired color:

<div class="diagram-wrapper">
  <app-circle color='blue'></app-circle>
  <app-circle color='green'></app-circle>
</div>

I hard-coded in the color values, but they could be provided as properties from the associated component.

The circle code then looks like this:

import { Component, OnInit, AfterViewInit, Input, ViewChild, ElementRef } from '@angular/core';

@Component({
  selector: 'app-circle',
  templateUrl: './circle.component.html',
  styleUrls: ['./circle.component.scss']
})
export class CircleComponent implements OnInit, AfterViewInit {
  @Input() color;
  @ViewChild('circle') circleEle: ElementRef;

  constructor() { }

  ngOnInit() { }

  ngAfterViewInit() {
    // Circle logic (fill colors, etc.)
    console.log(this.circleEle);
    if (this.circleEle) {
      this.circleEle.nativeElement.style.fill = this.color;
    }
  }
}

Hope this helps.

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

6 Comments

Marked as answer.
@DeborahK , I have a similar issue where all my child components seem to share the state data. I made a demo based on your demo. My file upload updates the topmost instance. Thx stackblitz.com/edit/…
Did I see in the stackblitz that you've solved your issue?
@AdamMendoza How did you managed to resolve your problem regarding the update of the topmost instance when uploading a file?
@Raluca The bug was that I had an extraneous name=“myField” in an *ngFor=“logic here” so the data was tied to all elements in the repeater. In my case I just had to delete the name property for the tag. If the name is needed then concatenate an index to reference each element individually
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.