首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角:更好的形式验证解决方案

角:更好的形式验证解决方案
EN

Stack Overflow用户
提问于 2016-08-04 13:49:41
回答 1查看 159关注 0票数 1

需要有关表单验证的建议。

我有这样的控制结构:

代码语言:javascript
复制
<form name="myForm">
    <control-wrap>
        <label isRequired="myForm.field1">Some text here</label>
        <custom-control name="field1" 
                  ng-required="true"
                  ng-something-else="any"
                  ng-model="modelForm.field1"></custom-control>
        <info>Some data after control</info>
        <error-list field="myForm.field1"></error-list>
    </control-wrap>

    <control-wrap>
        <label isRequired="myForm.field2">Some text here</label>
        <custom-control name="field2" 
                  ng-required="true"
                  ng-something-else="any"
                  ng-model="modelForm.field2"></custom-control>
        <info>Some data after control</info>
        <error-list field="myForm.field2"></error-list>
    </control-wrap>

    <control-wrap>
        <label isRequired="myForm.field3">Some text here</label>
        <custom-control name="field3" 
                  ng-required="true"
                  ng-something-else="any"
                  ng-model="modelForm.field3"></custom-control>
        <info>Some data after control</info>
        <error-list field="myForm.field3"></error-list>
    </control-wrap>
</form>

这是完全可怕的,unDRY和我想我做了一些非常错误的事情。

我想停止使用字段名,但我不知道如何以正确的方式将ngModel传递给兄弟(现在我被迫通过属性将ngModel传递给isRequired和错误列表)。

对我来说,ofc最好的解决方案是要求: '^ngModel‘来自isRequired和错误列表。

如有任何建议,将不胜感激。

P.S.我无法将字段存储在json对象中,字段之间有很多逻辑,标签和提示上也有不同的调整。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-05 07:55:51

嗯,我想出了一个解决方案:https://plnkr.co/edit/mPXpEaZs2uWZb3WRkmgp?p=preview

也许这不是最好的解决办法,但我不再需要名字了。

其主要思想是将模型引用设置为父容器,并从其他子容器中查看此引用。

所以最后我有:

代码语言:javascript
复制
    <control-wrap>
        <label link-required>Field1 label:</label>
        <input link-to-wrap ng-model="mc.field1" 
            type="text" 
            ng-required="true" 
            ng-minlength="5" 
            ng-maxlength="10" />
        <errors-list></errors-list>
    </control-wrap>

更新

关于使用模型存储验证规则的更多想法:

https://plnkr.co/edit/6ZVv685oSRDt7ELBKb9z?p=preview

新指令my-rulescontroller.js中的扩展数据

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38769241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档