gpt4 book ai didi

angular - 在输入属性中使用管道

转载 作者:太空狗 更新时间:2023-10-29 18:19:45 26 4
gpt4 key购买 nike

我正在使用一个 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"/>

但是我尝试使用管道:

<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" />

我收到这个错误:

Can't bind to 'data-bv-notempty-message' since it isn't a known property of 'input'. ("ut type="text" class="form-control" name="firstname" data-bv-notempty="true" [ERROR ->][data-bv-notempty-message]="'The first name is required' | 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);
}

}

方法:getTranslation()

public getTranslation(phrase:string):string {
return this.data && this.data[phrase] ? this.data[phrase] : phrase;
}

最佳答案

它会抛出错误,因为 Angular 不理解该属性名称。为了允许自定义属性在 Angular 上下文之外工作,您可以考虑添加 CUSTOM_ELEMENTS_SCHEMA 元素,这将是 HTML 上的任何其他自定义属性。

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

@NgModule({
declarations: [ ... ],
exports: [ ... ],
imports: [ ... ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class AppModule {}

您还可以使用属性绑定(bind),例如 [attr.something]="value"

[attr.data-bv-notempty-message]="'The first name is required and cannot be empty' | i18n"

关于angular - 在输入属性中使用管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46182259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com