具有动态属性的角度指令

时间:2018-02-23 22:11:04

标签: angular

我需要在Angular中的控件上渲染属性。将基于属性配置属性。用户将发送一个JSON文件,该文件将序列化为typescript中的对象。此打字稿文件将用作组件上的绑定。

{
      "row": 1,
      "col": 2,
      "key": "Input_2",
      "label": "First Name",
      "required": true,
      "type": 1,
      "hiddden": false,
      "maxLength": 100,
      "attributes": "required validateEqual='password' reverse='false'"
}


export class ControlBase<T> {
    row: number;
    col: number;
    value?: string;
    key: string;
    label: string;
    required: boolean;
    type: ControlType;
    hidden: boolean = false;
    attributes: string;
}

<input class="form-control" type="text" [(ngModel)]="value" {{control.attributes}}>

当我使用上面的绑定{{control.attributes}}时,我收到以下错误:无法在'Element'上执行'setAttribute':'{{control.attributes}}'不是有效的属性名。< / p>

1 个答案:

答案 0 :(得分:0)

我不认为有任何模板语法允许您设置动态数量的参数。但它可以通过代码实现 这是一个示例https://stackblitz.com/edit/angular-svcvsj

使用浏览器的开发者工具检查输入元素。属性应该在那里。我建议为属性使用强类型对象,以避免在那里进行额外的字符串解析

相关问题