0

This is my parent component(metric-details.component.html) where I'm trying to get the id number of a selected button and pass it to my child component. I'm not sure on the proper syntax to transfer the information which I think is my problem.

<h2>Visualized Software Metrics</h2>
<h2 *ngIf="domain?.catergory">Domain: {{ domain.catergory | uppercase }}</h2>
<h2 *ngIf="library?.name">Library: {{ library.name | uppercase }}</h2>
<div class = "row">
  <ul id = "thumbnailslist">
    <li *ngFor="let image  of visibleImages ">
      <a [routerLink] = "['/image', image.id]">
        <img src = "{{image.url}}" class = "tn">
      </a>
    </li>
  </ul>
</div>
<app-metric-view [domain] = "domain"></app-metric-view>

This is the corresponding .ts file(metric-details.component.ts)

import { Component, OnInit, Input } from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { Location } from '@angular/common';
import { ImageService } from './shared/image.service';
import { DomainService } from '../domain.service';
import { GraphService } from '../graph.service';
import { LibraryService } from '../library.service';
import { Domain } from '../library';
import { Library } from '../library';
import { map,mergeMap } from 'rxjs/operators';


@Component({
  selector: 'app-metric-details',
  templateUrl: './metric-details.component.html',
  styleUrls: ['./metric-details.component.css']

})
export class MetricDetailsComponent implements OnInit {

  visibleImages: any[] = [];
  activeId = 0;
  domain: Domain[];

  constructor(private imageService: ImageService, private domainService: DomainService, private libraryService:LibraryService, private graphService:GraphService, private location: Location, private route: ActivatedRoute,  private router: Router) {
    this.visibleImages = this.imageService.getImages();

}

  ngOnInit() {
    this.route.params.subscribe(params => { {this.activeId = +params['id'];
    console.log(this.activeId) //log the entire params object
    // console.log(params['id']) //log the value of id
    // const id = +this.route.snapshot.paramMap.get('id');
    // console.log(id);
    console.log(this.domainService.getDomain(this.activeId));
    this.domainService.getDomain(this.activeId).subscribe(domain =>this.domain = domain)
    console.log("YOOO",this.domain)

  }

This is my child component(metric-views.component.html) where I'm trying to get the id number that is stored in parent component in the variable domain

<div *ngIf="image">
  <h2>{{ image.catergory | uppercase }}</h2>
<div>

  <div *ngIf = "domain.id === 1" class ="row" >
    <button (click)="prev()" class="previous round">&#8249;</button>
      <div  [ngStyle] = "{'background-image': 'url('+ testing.url +')'}" class ="img-container">
      </div>
    <button (click)="next()" class="next round">&#8250;</button>
  </div>


  <div class ="row" >
      <button (click)="prev()" class="previous round">&#8249;</button>
      <div  [ngStyle] = "{'background-image': 'url('+ testing.url +')'}" class ="img-container">
      </div>
      <button (click)="next()" class="next round">&#8250;</button>
  </div>

</div>

<div class = "tab-rating">
  <ngbd-rating-template></ngbd-rating-template>
  <ngbd-tabset-basic></ngbd-tabset-basic>
</div>

This is the corresponding .ts file for the metric-view.component

import { Component, OnInit, Input} from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { Location } from '@angular/common';
import { ImageService } from '../metric-details/shared/image.service';
import { LibraryService } from '../library.service';
import { Library } from '../library';
import { Domain } from '../library';
import { GraphService } from '../graph.service';
import { DomainService } from '../domain.service';
import { map,mergeMap } from 'rxjs/operators';

@Component({
  selector: 'app-metric-view',
  templateUrl: './metric-view.component.html',
  styleUrls: ['./metric-view.component.css']
})
export class MetricViewComponent implements OnInit {
image: any;
testing: any;
visibleImages: any[] = [];



@Input() domain: any[];
// activeId = 0;



  constructor(private imageService: ImageService, private libraryService:LibraryService, private domainService: DomainService, private graphService:GraphService, private location: Location, private route: ActivatedRoute, private router: Router) {
    this.visibleImages = this.imageService.getImages();
   }

  ngOnInit() {
    this.testing =  this.graphService.getTestingGraphs(domain.id);
    console.log(this.testing);
    this.image = this.imageService.getImage(domain.id);
    console.log(this.image);
}

  next() {
    // const next = this.activeId + 1 >= this.image.length - 1  ? this.graph.length - 1 : this.activeId + 1;
    const next = this.activeId + 1 >= 9  ? 1 : this.activeId + 1;
    this.router.navigate(['/image/' + next]);
  }

  prev() {
    const prev = this.activeId - 1 < 1 ? 9 : this.activeId - 1;
    this.router.navigate(['/image/' + prev]);
  }

}
pennyBoy
  • 397
  • 2
  • 17
  • 2
    Possible duplicate of [How do I share data between components in Angular 2?](https://stackoverflow.com/questions/31026886/how-do-i-share-data-between-components-in-angular-2) – Supamiu Apr 06 '18 at 08:48
  • Yeah it might be a possible duplicate but I still don't fully grasp the concepts and understand how to implement it. – pennyBoy Apr 06 '18 at 08:51
  • @Supamiu is not possible to get help if it is consider a duplicate? – pennyBoy Apr 06 '18 at 09:06
  • The syntax is `[inputName]="theValueToPassAsInput"`. Your input is named domain, so it should be `[domain]="theValueToPassAsInput"`. Assuming what you want to pass is the parent component's domain field, it should thus be `[domain]="domain"`. If you didn't use `any` as your types, we wouldn't have to make so many guesses. – JB Nizet Apr 06 '18 at 09:12
  • @JBNizet inside of my parent component metric-detail.component.html I did pass the domain field like this however when I attempt to run the code I get an error that domain is not defined – pennyBoy Apr 06 '18 at 09:16
  • That's normal and expected. The parent component is displayed, and passes the domain to its child immediately. But the domain is undefined, and is only populated later, asynchronously, when the response to the call `this.domainService.getDomain(this.activeId)` comes back from the server. So you need to decide what to do while the domain is not available yet. – JB Nizet Apr 06 '18 at 09:25
  • Can you share a plunker link? – Karthick Manoharan Apr 06 '18 at 09:38
  • @KarthickManoharan I'm not sure how to do that. – pennyBoy Apr 06 '18 at 09:42
  • @KarthickManoharan http://plnkr.co/edit/EYX3oKP2EETDTNxeRsHz?p=options is that appropriate? – pennyBoy Apr 06 '18 at 09:48

0 Answers0