多个自定义值访问器与表单控件未指定名称属性 NG_VALUE_ACCESSOR 匹配

问题描述 投票:0回答:1

多个自定义值访问器与表单控件未指定名称属性 NG_VALUE_ACCESSOR 匹配

我为输入字段创建了两个指令,一个复选框 true 或 false 而不是 0 或 1 。另一个指令选项值我需要数字而不是字符串

Directive1 : 
@Directive({selector: DI_1, 
providers: [     
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Directive1),
multi: true     
}   
]})


Directive2 : 
@Directive({selector: DI_2,
 providers: [     
{       provide: NG_VALUE_ACCESSOR, 
        useExisting: forwardRef(() => Directive2),
        multi: true     
}   
]})

如果我要删除提供商。它工作正常。我需要两个指令如何解析角度

angular typescript angular17
1个回答
0
投票

尽量不要使用提供者,否则以这种方式在构造函数中注入 ngControl

@Directive({selector: DI_1, 
//remove the providers
/*providers: [     
  {
   provide: NG_VALUE_ACCESSOR,
   useExisting: forwardRef(() => Directive1),
   multi: true     
  }   
]
*/
})
export class Directive1
{
  constructor(public ngControl: NgControl) {
    if (ngControl) {
        ngControl.valueAccessor = this;
    }
  }
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.