2014-09-11 121 views
0

我想創建一個指令與input。此input可能可能需要required屬性。如何將「required」屬性動態添加到指令模板?

例如

<my-input name="test" required/> 

<my-input name="test" /> 

現在,在我的指導我有這樣的一個模板:

<input type="text" name="{{name}}" /> 

現在,如果我添加驗證工作正確的模板內required屬性。但我當然需要動態地添加它。所以我使用下面的代碼來添加屬性。

if (attributes["required"] != undefined) 
{ 
    var input = element.find("input"); 
    input.prop("required", true); 
} 

我曾嘗試把這個代碼linkpre compile內部和HTML渲染正確,但是,這並不工作。

我是新的指令,所以我顯然是在討論這個錯誤。

我應該如何添加required屬性?

回答

0

我已經找到了如何在指令中做到這一點,而不是讓角度機制工作。基本上只使用正常的驗證技術。

if (attributes["required"] != undefined) 
{ 
    controller.$setValidity("required", !(scope.Model.Value == null || scope.Model.Value.length === 0 || scope.Model.Value.trim() === "")); 
}