I'm learning Angular 6 and when learning how to pass data from a parent component into a child component it was shown that we should add an Input() property into the child which is what the parent will populate.
My question was, if i'm just creating a component with a property, like elementId, that it going to be set when i use the element. It will not be passed into it from another component, but hardcoded like:
<my-component elementId='xyz'>
the guide i'm seeing is creating that elementId as an Input() in the component. I thought earlier guides i read never did that, it was just declared as a regular property in the component class, without the Input()
Is the Input() always needed, or just when passing data from parent into child?
I have included the code the author used:
import { Component, OnInit, Input } from '@angular/core';
import { Title } from '@angular/platform-browser';
@Component({
selector: 'simple-modal',
templateUrl: './simple-modal.component.html',
styleUrls: ['./simple-modal.component.css']
})
export class SimpleModalComponent implements OnInit {
@Input() title: string;
@Input() elementId: string;
constructor() { }
ngOnInit() {
}
closeModal() {
}
}
Component is used within another component as:
<simple-modal elementId="searchResults" title="Matching Sessions">
<div class="list-group">
<a class="list-group-item" *ngFor="let session of foundSessions" [routerLink]="['/events', session.eventId]">{{session.name}}</a>
</div>
</simple-modal>
elementIdinsidemy-componentif you don't use@Input()?