【问题标题】:Set empty attribute to polymer component inside another one将空属性设置为另一个内部的聚合物组件
【发布时间】:2015-02-05 10:08:57
【问题描述】:

我一直在尝试为另一个聚合物元素中的聚合物元素设置一个空属性。

这是我的自定义元素的代码。只要 custom-core-input 的 required 属性为 true,我就将“required”设置为 input 元素。

<polymer-element name="custom-core-input" attributes="columnId inputError validation required">
<template>
    <section>
        <paper-input-decorator id="decorator" error="{{inputError}}">
            <input id="custominput" is="core-input" validate="{{validation}}" on-change="{{inputCommited}}">
        </paper-input-decorator>
    </section>
</template>
<script>
    Polymer({
        inputCommited: function () {
            this.$.decorator.isInvalid = !this.$.custominput.validity.valid;
        }
    });
</script>

到目前为止,我已经尝试访问输入元素并从脚本中设置“attr”,我认为它不起作用,但值得一试。我只是不知道如何解决这个问题,我觉得必须有一个简单的答案但想不出。

另外(并且不相关),我认为我做错了其他事情,因为纸输入装饰器不会“获取” inputError 值。

感谢阅读:)

【问题讨论】:

    标签: polymer


    【解决方案1】:

    Required 是一个conditional attribute,所以你可以像这样在input 元素上设置它:

    <polymer-element name="custom-core-input" 
          attributes="columnId inputError validation required">
      <template>
        <section>
          <paper-input-decorator id="decorator"
                error="{{inputError}}"
                autovalidate>
            <input id="custominput" is="core-input"
                  validate="{{validation}}"
                  on-change="{{inputCommited}}"
                  required?="{{required}}">
          </paper-input-decorator>
        </section>
      </template>
      <script>
        Polymer('custom-core-input', {
          required: false
        });
      </script>
    </polymer-element>
    

    请注意,custom-core-inputrequired 属性必须初始化为 false(或 true,取决于您的默认设置)。

    仅当输入无效时才会显示错误消息。所以一种选择是设置autovalidate 属性。在您的custom-core-input 上设置requiredinputError,您将在页面加载时看到错误消息。更一般地,您希望根据当前输入的有效性将 isInvalid 设置为 true 或 false。

    【讨论】:

    • 我特别想避免的是自动验证,因为几乎所有字段都标记为红色的类似表单的控件看起来不太好。我希望它在输入值提交后立即验证,我用脚本更新了我的问题。我会将您的答案标记为正确,因为它解决了线程主要问题,但如果您对此的“验证”方面有任何想法,我将不胜感激。谢谢:)
    • 澄清一下,当我说错误消息没有被“放入”纸张元素时,我的意思是,在检查最终代码时,它显示 error="{{inputError}}" ,而不是其中的值,就像在任何其他属性上发生的那样,我不希望看到错误消息,就像验证之外的那样。
    • 您会看到error="{{inputError}}",因为error 本身就是Polymer 元素(不是普通的DOM 元素)的属性。为了使属性绑定正常工作,此 Polymer 表达式不会替换为其结果。评估将推迟到此属性呈现在paper-input-decorator 内的div.error-text 元素中。
    【解决方案2】:

    这是一种方法,在元素原型中:

    <script>
            Polymer({
                ready: function () {
                    //check the "custom-core-input" required attribute:
                    if (this.required)
                        //select the input element by Id, and set Required attr to True:
                        this.$.custominput.required = true;
                }
            });
    </script>
    

    【讨论】:

    • 我想我之前尝试过,但也不管用。我在想问题可能不是所需的 attr,而是我没有正确管理 on-change 事件,以便装饰器对验证做出反应。我正在用脚本中的内容更新主帖。不过,我似乎没有找到让错误属性正确获取其值的方法。
    猜你喜欢
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2017-08-20
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多