在输入属性中使用管道

Por*_*koi 4 angular

我正在使用UI框架:SmartAdmin

这提供了国际化功能 i18n

我正在尝试使用此Boostrap验证模块。

如果我这样说,那就行得通:

 <input type="text" class="form-control" name="firstname" data-bv-notempty="true" 
 data-bv-notempty-message="The first name is required and cannot be empty"
 data-bv-stringlength-max="50" data-bv-stringlength-message="The first name must be less than 50 characters long"/>
Run Code Online (Sandbox Code Playgroud)

但是我尝试使用管道:

<input type="text" class="form-control" name="firstname" data-bv-notempty="true" 
data-bv-notempty-message="{{'The first name is required and cannot be empty' | i18n}}"
data-bv-stringlength-max="50" data-bv-stringlength-message="The first name must be less than 50 characters long" />
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

无法绑定到“ data-bv-notempty-message”,因为它不是“ input”的已知属性。(“ ut type =” text“ class =” form-control“ name =”名字“ data-bv-notempty =” true“ [错误->] [data-bv-notempty-message] =”'必需的| | i18n“ data-bv-stri”):ng:///UserModule/UserAccountComponent.html@66:22

问题:如何在输入属性中使用管道?

编辑:添加代码管道:

import { Pipe, PipeTransform } from '@angular/core';
import {I18nService} from "./i18n.service";

@Pipe({
  name: 'i18n',
  pure: false
})
export class I18nPipe implements PipeTransform {

  constructor(public i18nService: I18nService){}

  transform(phrase: any, args?: any): any {
    //console.log(phrase)
    return this.i18nService.getTranslation(phrase);
  }

}
Run Code Online (Sandbox Code Playgroud)

方法:getTranslation()

public getTranslation(phrase:string):string {
    return this.data && this.data[phrase] ? this.data[phrase] : phrase;
}
Run Code Online (Sandbox Code Playgroud)

Pan*_*kar 5

因为Angular不了解该属性名称,所以引发了错误。要允许自定义属性在不存在角度的上下文中工作,可以考虑添加CUSTOM_ELEMENTS_SCHEMAelement,它将在HTML上的任何其他自定义属性。

import { CommonModule } from '@angular/common';

@NgModule({
  declarations: [ ... ],
  exports: [ ... ],
  imports: [ ... ],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

您还可以使用属性绑定 [attr.something]="value"

[attr.data-bv-notempty-message]="'The first name is required and cannot be empty' | i18n"
Run Code Online (Sandbox Code Playgroud)