在Aurelia的子类上使用@bindable属性 [英] Using the @bindable attribute on child class in Aurelia
问题描述
对于当前的项目,我们正在创建许多自定义控件,它们共享相同的属性并且可以绑定.
For the current project at work, we are creating quite a few custom controls that all share the same properties and that are bindable.
@bindable required: boolean = false;
@bindable minLength: number = 0;
@bindable maxLength: number = 0;
由于我们要避免代码重复,因此我们的想法是将这些可绑定属性保留在一个单独的类中,在本例中称为"Validation".
Since we'd like to avoid code duplication, the idea is to keep these bindable properties in a separate class, called 'Validation' in this case.
import {Validation} from "./validation";
export class MyClass {
private validation: Validation = new Validation();
// also tried:
// @bindable validation: Validation = new Validation();
}
问题是如何使HTML绑定到Validation类中的属性.这样做validation.required.bind="someProperty.required"
不会更新验证实例上的required
属性.我们尝试使用DI,但这似乎也没有减少.
The question is how to get the HTML to bind to the properties in the Validation class. Doing this validation.required.bind="someProperty.required"
doesn't update the required
property on the validation instance. We attempted to use DI, but that didn't seem to cut it either.
import {inject} from "aurelia-framework";
import {Validation} from "./validation";
@inject(Validation)
export class MyClass {
constructor(private validation: Validation) {
this.validation = validation;
}
}
任何提示将不胜感激.
Aurelia似乎将"validation.required"解释为命令而不是表达式.
It seems that Aurelia interprets 'validation.required' as a command rather than an expression.
WARN [templating-binding] Unknown binding command. Object {defaultBindingMode: null, attrName: "validation", attrValue: "true", command: "required", expression: null}
推荐答案
作为一种变通办法,直到Aurelia支持具有可绑定属性的继承,我将绑定到具有某些共享属性的类.可绑定的控件现在将在各个控件之间重复.
As a work-around until inheritance with bindable properties gets supported in Aurelia, I am binding to a class that has some of the shared properties. The bindable ones will get duplicated across the controls for now.
import {bindable, bindingMode} from "aurelia-framework";
import {IControlBase, ControlBase} from "./controlbase";
export class MyClass {
@bindable controlbase: IControlBase = new ControlBase();
@bindable label: string = "";
@bindable editing: boolean = false;
@bindable({ defaultBindingMode: bindingMode.twoWay })
value: string;
}
这篇关于在Aurelia的子类上使用@bindable属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!