如何基于其他属性

时间:2015-11-16 10:53:47

标签: javascript polymer-1.0

我希望函数的Polymer对象设置属性值,如果未设置默认值,则取决于其他属性

Polymer({
    is: "my-element",
    properties : {
        a: String,
        b: String,
        c: {
            type: String,
            value: function() {return " " + this.a + " " + this.b}
        }
    },
    calcC: function() {return " " + this.a + " " + this.b}
});

我怎么能意识到它?

<dom-module id="my-element">
    <template>
        <p>a: {{a}}</p>
        <p>b: {{b}}</p>
        <p>c: {{c}}</p>
        <p>calc c: {{calcC()}}</p>
    </template>
</dom-module>

<my-element a="a" b="b"></my-element>

渲染

a: a

b: b

c: undefined undefined

calc c: undefined undefined

jsfiddle

1 个答案:

答案 0 :(得分:0)

怎么样

HTML

<dom-module id="my-element">
    <template>
        <p>a: {{a}}</p>
        <p>b: {{b}}</p>
        <p>c: {{c}}</p>
    </template>
</dom-module>

<my-element a="a" b="b"></my-element>
<my-element a="a" b="b" c="c"></my-element>

JS

Polymer({
    is: "my-element",
    properties: {
        a: String,
        b: String,
        c: String
    },
    ready: function () {
        if (this.c == undefined) {
            this.c = " " + this.a + " " + this.b;
        }
    }
});

输出

  

a:a

     

b:b

     

c:a b

     

a:a

     

b:b

     

c:c