0

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?

7
  • 1
    Is it also undefined in ngOnInit and/or other lifecycle hooks? Commented Nov 7, 2019 at 19:08
  • I tested against ngAfterViewInit and it is still undefined. Commented Nov 7, 2019 at 19:09
  • 1
    You can check this stackblitz and try to find what you are doing differently. Commented Nov 7, 2019 at 19:34
  • 1
    Also, please note that using [objectId]="abcdef" will give undefined. We assume that you actually pass [objectId]="123456". Commented Nov 7, 2019 at 19:45
  • 1
    No, because the square brackets tell Angular that it should evaluate the expression abcdef, which is undefined (unless you define the property abcdef in your component class). To bind a literal string, you can use objectId="abcdef" or [objectId]="'abcdef'". Commented Nov 7, 2019 at 19:52

2 Answers 2

2

objectId is a string, and as in input you can pass as either:

<!-- use non-bracket notation to pass as string -->
<main-component objectId="123456"></main-component>

or

<!-- use bracket notation with single quotes to pass as string -->
<main-component [objectId]="'123456'"></main-component>
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think that it would look for a variable with the name 123456. It would evaluate the expression 123456, which would result in the corresponding number. That is why the original code should work with 123456 but not with abcdef.
2

objectId is undefined when queried in the constructor but should be defined if you were to implement the OnInit interface and look at it in ngOnInit(). Input passing doesn't happen when the component class is constructed, it's somewhere further down the lifecycle.

2 Comments

Thanks for the suggestion but it is still undefined.
Are you initially passing the input as undefined and switching it later? If so you might need to implement OnChanges and check new values in ngOnChanges()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.