1

I'm wondering if it's possible to somehow insert default values into a component from a library (but not wrapping it into another component which does this) to set default values.

e.g. i don't want this same thing all over my code, so when something changes that I have to change all the occurrences:

    <lib-datepicker
      [mindate]="01-01-2020"
      [pattern]="dd-MM-yyyy"
      [icon]="fa-calendar" ... />
    

i'd somehow like to do something like this:

    <lib-datepicker
      some_angular_magic.insertDatepickerDefaultValues() ... />

Is there something similar which does this?

2
  • I'm assuming this component is not yours but external library, and cannot make changes to it? Commented Sep 2, 2024 at 14:20
  • I mean i could, but it's used in other projects and i don't want to affect others with it Commented Sep 2, 2024 at 14:31

2 Answers 2

0

Inputs on components can have a default value.

export class ExampleComponent {
  @Input() stringValue = 'default';
}

Them means that usages of the component can simply omit passing a value for a particular input and the default value will be used.

For example:

<app-example></app-example>
<app-example stringValue="test"></app-example>
Sign up to request clarification or add additional context in comments.

Comments

0

You can create a directive with the same selector as the component, then you can override the input properties on the directive constructor.

I recommend using the Wrapper component method since it's tried and tested for many years, if not try this:

import { Component, Input, Directive, inject } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';

@Directive({
  selector: 'app-child',
  standalone: true,
})
export class ChildDirective {
  child = inject(Child);

  constructor() {
    this.child.mindate = 'directive set this!';
    this.child.pattern = 'directive set this!';
    this.child.icon = 'directive set this!';
  }
}

@Component({
  selector: 'app-child',
  standalone: true,
  template: `
    {{mindate}} | {{pattern}} | {{icon}}
  `,
})
export class Child {
  @Input() mindate = 'nothing was set';
  @Input() pattern = 'nothing was set';
  @Input() icon = 'nothing was set';
}

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [ChildDirective, Child],
  template: `
    <app-child/><br/><br/>
    <app-child
    [mindate]="'input from parent'"
[pattern]="'input from parent'"
[icon]="'input from parent'"/><br/><br/>
    <app-child/><br/><br/>
    <app-child/>
  `,
})
export class App {
  name = 'Angular';
}

bootstrapApplication(App);

Stackblitz Demo

2 Comments

Thank you, by "I recommend using the parent component method...", do you mean wrapping it in a component?
@Wilbert Yes, since its tried and tested and guaranteed to work, this method is rarely used by developers

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.