简体   繁体   English

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

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

Is it possible in Aurelia to use vanilla js setAttribute() with custom attributes? 在Aurelia中是否可以将Vanilla js setAttribute()与自定义属性一起使用? When I inspect the dom, the change is made on the custom element, but it doesn't seem to trigger anything in my model or view no matter what I try. 当我检查dom时,更改是在custom元素上进行的,但是无论我尝试什么,它似乎都不会触发模型中的任何内容或视图。 Is there a "best practice" way to do this? 是否有“最佳实践”方法来做到这一点?

home.ts home.ts

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

home.html 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>

now-loading-circle.ts 现在加载圈子

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

now-loading-circle.html 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>

The simplest way to do this is to use data-binding. 最简单的方法是使用数据绑定。 Use color.bind instead of setting the attribute. 使用color.bind而不是设置属性。 If you set the attribute value explicitly, then in my opinion you are not leveraging Aurelia to its full extent. 如果您明确设置属性值,那么我认为您没有充分利用Aurelia。

This is what you can do. 这就是您可以做的。

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

And then use the color in data-binding: 然后在数据绑定中使用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>

Hope this helps. 希望这可以帮助。

No, Aurelia currently does not seem to support binding to custom attributes. 不,Aurelia当前似乎不支持绑定到自定义属性。

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

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

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