I'm storing some values in an array domain:Domain[], if I wanted to transfer the array from a parent's component to a child's component by using the child's selector inside of my parents html file then declaring the decorator @Input() domain: Domain[]; in my child's component to get the contents inside the array should I be able to use domain.id if I wanted inside a function in my child's component? I'm just having a hard time grasping this concept.
This is the stackblitz file: https://stackblitz.com/github/pennyfea/Project3-HCI
This is my parent's component where I'm storing the initial data in the array. Then I would try to pass it over to the child's component using the child's selector inside of the parent's html file. Something I've seen on a number of other questions and tutorials.
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("Added",this.domain);});
this.domainService.getDomain(this.activeId).subscribe(domain => {this.domain = domain; console.log(this.domain);})
}
}
<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 my child's component where I want to get the information from my parent's component using the input decorator, then using the information from the array say the domain.id inside a function.
<div *ngIf="image">
<h2>{{ image.catergory | uppercase }}</h2>
<div>
<div *ngIf = "domain.id === 1" class ="row" >
<button (click)="prev()" class="previous round">‹</button>
<div [ngStyle] = "{'background-image': 'url('+ testing.url +')'}" class ="img-container">
</div>
<button (click)="next()" class="next round">›</button>
</div>
<!-- <div class ="row" >
<button (click)="prev()" class="previous round">‹</button>
<div [ngStyle] = "{'background-image': 'url('+ testing.url +')'}" class ="img-container">
</div>
<button (click)="next()" class="next round">›</button>
</div>
</div> -->
<div class = "tab-rating">
<ngbd-rating-template></ngbd-rating-template>
<ngbd-tabset-basic></ngbd-tabset-basic>
</div>
<!-- <div *ngIf="domain.id != 1" class ="row" >
<button (click)="prev()" class="previous round">‹</button>
<div [ngStyle] = "{'background-image': 'url('+ testing.url +')'}" class ="img-container">
</div>
<button (click)="next()" class="next round">›</button>
</div> -->
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: Domain[];
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]);
}
}