如何使表达'&' - 与可选的初始化

时间:2017-10-18 11:57:18

标签: angularjs typescript angularjs-components

我想初始化一个回调& -property绑定到一个返回true的函数(如果没有传递回调)。像这样:

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 });
        }
    },
}

从未定义validityCheck属性的组件调用validate()时,它将返回undefined。关于如何使其发挥作用的任何想法?

2 个答案:

答案 0 :(得分:3)

尝试将绑定设为可选:

bindings: {
    validityCheck: '&?'
},

答案 1 :(得分:-1)

正如术语binding所暗示的那样,它必须从父组件传入。

<date-picker-input-component
  format="some string"
  mode="some other string"
  validity-check="someFunctionFromParentController">
</date-picker-input-component>

从控制器中删除此部分

validityCheck: Function = (args) => { return true; }; // <- doesn't work

假设someFunctionFromParentController在您的父控制器上定义

相关问题