2017-09-12 67 views
2

我使用的UI框架:SmartAdmin在輸入使用管道attribut

這提供了一個國際化功能與i18n

我試圖用它這個自舉驗證模塊。

如果我把這個,它的工作:

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

但是II嘗試使用管道:

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

我得到這個錯誤:

不能結合'data-bv-notempty-message',因爲它不是'input'的已知 屬性。 (「ut type =」text「class =」form-control「 name =」firstname「data -bv-notempty =」true「 [ERROR - >] [data-bv-notempty-message] =''第一個名稱是必需的」 |國際化 「 數據BV-STRI」):NG:///UserModule/[email protected]:22

問題:在輸入attribut如何使用管道?

編輯:添加代碼管:

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; 
} 
+1

結合請使用angularJs標籤。角標籤是爲角2+ :-) – alexKhymenko

+1

@alexKhymenko它正確地被標記..它是有角度的2+問題 –

+1

@Portekoi對不起,角2中的綁定是不同的。假設它是angular.js。 – alexKhymenko

回答

1

因爲角度不明白,屬性名稱,則拋出一個錯誤。爲了允許自定義屬性在超出角度上下文的範圍內工作,您可以考慮添加CUSTOM_ELEMENTS_SCHEMA元素,這將成爲HTML上的任何其他自定義屬性。

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

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

你也可以使用屬性喜歡[attr.something]="value"

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