0

I want to set @Input parameters optional.

For example:

Child1.html:

templateUrl:'common-html.html'

Child1.TS:

currentValue : boolean = true;

CASE 2:

Child2.html:

templateUrl:'common-html.html'

Child2.TS:

// Not declared **currentValue**

Common-HTML.html:

<app-master [isLogEnabled]="currentValue"></app-master>

But above changes giving an error while AOT:

Property 'isLogEnabled' does not exist on type Child2.Component

So I cannot change HTML of :

<app-master [isLogEnabled]="currentValue"></app-master>
7
  • You cannot do <app-master></app-master>? Commented May 19, 2020 at 15:59
  • Actually, that HTML is used as a centralized, so I cannot change Commented May 19, 2020 at 15:59
  • The question is unclear at the moment. But answering the question of "optional input properties", you could try isLogEnabled?: any in the child component. Commented May 19, 2020 at 16:02
  • Added more info Commented May 19, 2020 at 16:03
  • Probably not as clean, but you could pass an object instead, with optional properties. Commented May 19, 2020 at 20:25

2 Answers 2

1

I would do:

App-Master-Component.ts

@Input() isLogEnabled: boolean = false; // can change false to true, null or w.e. depending on what you want as the default
// if consumer passes in a value, it will be overridden

Then in Child2.component.html

<app-master></app-master>

You cannot bind [isLogEnabled]="currentValue" if currentValue does not exist in the Child2.component.ts file.

Sign up to request clarification or add additional context in comments.

1 Comment

The HTML is common so I can not change the HTML
0

You can't provide property that you didn't declare. But you can use something like this:

<app-master [isLogEnabled]="currentValue || false"></app-master>

Or inside component you can create getter:

get isLogEnabled() {
    return this.isLogEnabled || false;
}

Or you can try to use '?'

@Input() isLogEnabled?: boolean;

Comments

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.