繁体   English   中英

在Aurelia自定义属性上使用setAttribute()

[英]Using setAttribute() on an Aurelia custom attribute

在Aurelia中是否可以将Vanilla js setAttribute()与自定义属性一起使用? 当我检查dom时,更改是在custom元素上进行的,但是无论我尝试什么,它似乎都不会触发模型中的任何内容或视图。 是否有“最佳实践”方法来做到这一点?

home.ts

export class Home{
    public onButtonClicked():void{
        document.getElementById('test123').setAttribute('color', 'green');
    }
}

home.html

<template>
    <require from="../../elements/now-loading-circle/now-loading-circle"></require>
    <button click.delegate="onButtonClicked()">Click</button>
    <now-loading-circle id="test123" color="red"></now-loading-circle>
</template>

现在加载圈子

import {bindable, autoinject} from 'aurelia-framework';
@autoinject
export class NowLoadingCircle{
    @bindable color:string;
    public colorChanged(newValue):void{
        alert(newValue);
    }
}

now-loading-circle.html

<template>
    <svg viewBox="0 0 120 120">
        <circle repeat.for="circ of coords" cx="${circ.x}" cy="${circ.y}" r="${smallCircleRadius}" class="circ-${$index + 1}"></circle>
    </svg>
</template>

最简单的方法是使用数据绑定。 使用color.bind而不是设置属性。 如果您明确设置属性值,那么我认为您没有充分利用Aurelia。

这就是您可以做的。

export class Home{
    color: string; // have a member in Home.
    public onButtonClicked():void{
        this.color = 'green';
    }
}

然后在数据绑定中使用color

<template>
    <require from="../../elements/now-loading-circle/now-loading-circle"></require>
    <button click.delegate="onButtonClicked()">Click</button>
    <now-loading-circle id="test123" color.bind="color"></now-loading-circle>
</template>

希望这可以帮助。

不,Aurelia当前似乎不支持绑定到自定义属性。

https://github.com/aurelia/binding/issues/380

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM