2017-08-23 32 views
0

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

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> 

现在加载-circle.ts

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

现在加载-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> 
+0

你尝试使用的setAttribute()?它怎么样? – zynkn

+0

@YoungKyunJin是的,当我检查dom时,更改是在自定义元素上进行的,但无论我尝试什么,它都不会在我的模型或视图中触发任何内容。 –

+0

我可以看到你的代码吗? – zynkn

回答

0

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

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

+0

感谢您的回复。我的问题不是绑定到自定义属性。这是关于通过元素内部的'setAttribute'响应属性,根据该链接,该属性应该工作。查看您发布的链接中的第二到最后一个回复。 –

+0

我认为你误解了我给你的链接中的回复。在那个例子中,他手动更新元素属性以反映属性更改。你正在寻找的是完全相反的,你希望模型值在属性改变时更新,这在Aurelia目前是不可能的。 Dom属性和Node属性之间有区别,请参阅:https://stackoverflow.com/questions/6003819/properties-and-attributes-in-html – user1304988

0

最简单的方法是使用数据绑定。使用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> 

希望这有助于。

+0

我很感谢您的回应。我知道这种方法,但我特别问是否甚至可以使用'setAttribute()'来更新组件。几乎所有的组件式框架和库都可以响应'setAttribute()'。我只是想知道Aurelia是否也有。 –