I've built an Angular library that I can import into outside applications. Withing my library exists a component called 'MainComponent' that has a single @Input variable for 'objectId'.
import { Component, Input } from "@angular/core";
import { Router } from "@angular/router";
@Component({
selector: "main-component",
templateUrl: "../templates/app.html",
styleUrls: ["../styles/app.css"],
providers: []
})
export class MainComponent {
@Input() objectId: string;
constructor() {
console.log("MainComponent constructor running!! objectId: " + this.objectId);
// 'objectId' is undefined in the constructor
}
}
When I import the library into another project, I use the MainComponent like this:
<main-component [objectId]="123456"></main-component>
However, objectId is always undefined. I'm not sure what I'm doing wrong - is there something different I must do since this is a custom made Angular library?
undefinedinngOnInitand/or other lifecycle hooks?[objectId]="abcdef"will giveundefined. We assume that you actually pass[objectId]="123456".abcdef, which isundefined(unless you define the propertyabcdefin your component class). To bind a literal string, you can useobjectId="abcdef"or[objectId]="'abcdef'".