2016-07-26 59 views
0

上相同的值我有一個簡單的表格有兩個文本輸入象下面這樣:2 NG-模型形式

<form> 
    // this is visible in mobile view 
    <input id="mobileView" type="email" required ng-model="myValue" /> 

    // this is visible on desktop view 
    <input id="desktopView" type="email" required ng-model="myValue" /> 
</form> 

我的問題是不這樣做違反角表單驗證?因爲兩個輸入都在DOM中,並且在一個視圖中,其中一個輸入具有價值,而在其他視圖中,它沒有任何價值。這是否打破了角度的驗證?

+0

沒有問題,您的代碼,沒關係。 – dfsq

+1

@SasankSunkavalli ng-hide/ng-show只添加/刪除樣式'display:none',所以在這裏不會有幫助,因爲這兩個輸入都將以這種形式註冊。如果你想根據視圖註冊'input',你應該使用'ng-if'。 – krutkowski86

+0

@ krutkowski86感謝您的信息 –

回答

1

您的代碼是正確的,並使用ng - 如果它處理DOM元素。

<form> 
    // this is visible in mobile view 
    <input id="mobileView" type="email" ng-if="condition for mobile view" required ng-model="myValue" /> 

    // this is visible on desktop view 
    <input id="desktopView" type="email" ng-if="condition for desktop view" required ng-model="myValue" /> 
</form>