Search code examples
angularjstypescriptangularjs-components

How to make expression '&'-binding with optional initialization


I want to initialize a callback &-property binding to a function that returns true (if no callback was passed). Something like this:

export const DatepickerInputComponent = {
    bindings: {
        validityCheck: '&',
    },
    templateUrl: './datepicker-input.component.html',
    controller: class DatepickerInputController implements ng.IController {
        validityCheck: Function = (args) => true; // <- doesn't work
        validate(value: Date): void {
            // some validations
            return this.validityCheck({ selection: value });
        }
    },
}

When calling validate() from a component with no validityCheck property defined, it will return undefined. Any ideas on how to make it work?


Solution

  • Try making your binding optional:

    bindings: {
        validityCheck: '&?'
    },