Angular-formly:为什么指令的行为与类型不同?

Angular-formly: Why does the directive behave differently from the type?

我有两个输入字段,都是由同一个模板生成的。我正在设置

...    
templateOptions: {
  ...
  required: true
}

使用 formlyConfig.setType 注册了一个输入字段 另一个使用指令。我创建了一个 JS Bin here.

只有第一个获得了所需的属性,第二个没有。在自定义模板(控制器选项)的 docs 中,它说:

Provides you the ability to add custom behavior to the type without having to make an entire directive (you can make a directive instead if you wish).

我做错了什么?

第二个示例中的问题是,当 angular-formly 使用模板处理选项时,所有 angular-formly 看到的是:模板的 <plain-text>。因此,它不知道将 required 属性放在哪个元素上。 angular-formly 只会将这些属性附加到具有 ng-model 的元素上,因为这些是该属性唯一有意义的元素。

同样,这里的关键是 angular-formly 在处理模板时(在编译之前)看到的内容。指令编译成什么并不重要。所以是的,你可以使用你自己的指令,但是如果你想利用 angular-formly 的特性,它需要利用 ng-model 控制器(就像 this example 中使用的指令)。

祝你好运!