表单上具有相同值的两个 ng-model

two ng-model with the same value on a form

我有一个简单的表单,其中包含两个文本输入,如下所示:

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

我的问题是这样做是否违反了 angular 表单验证?因为两个输入都在 DOM 中,并且在一个视图中,其中一个具有价值,而在另一个视图中,它没有任何价值。这会破坏 angular 的验证吗?

您的代码是正确的,如果它处理 DOM 元素,请使用 ng-。

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