我正在玩聚合物2.0,我不明白如何将对象作为元素属性传递。将对象传递给聚合物2中的元素属性
这里是我的代码:
<dom-module id="notes-app">
<template>
<style>
:host {
display: block;
}
</style>
<button on-click="loadNotes">Get the notes</button>
<template is="dom-repeat" items="[[notes]]" as="note">
<note recipe='JSON.stringify(note)'></note>
</template>
</template>
<script>
class NotesApp extends Polymer.Element {
static get is() { return 'notes-app'; }
static get properties() {
return {
notes: {
type: Array,
value: []
}
};
}
loadNotes() {
this.notes = [
{"desc":"desc1", "author":"auth1", "type":"type1"},
{"desc":"desc2", "author":"auth2", "type":"type2"},
{"desc":"desc3", "author":"auth3", "type":"type3"}
];
}
}
window.customElements.define(NotesApp.is, NotesApp);
</script>
</dom-module>
simple-note
是谁拥有Object
类型的属性的元素:
<dom-module id="note">
<template>
<style>
:host {
display: block;
}
</style>
<div>
<fieldset>
<label>description</label>{{note.desc}}<br>
<label>author</label>{{note.author}}<br>
<label>type</label>{{note.type}}
</fieldset>
</div>
</template>
<script>
class SimpleNote extends Polymer.Element {
static get is() { return 'simple-note' }
static get properties() {
return {
note: {
type: Object,
value: {},
notify: true
}
};
}
}
customElements.define(SimpleNote.is, SimpleNote);
</script>
</dom-module>
正如你可以看到我想要note-app
在其notes
属性显示的所有对象通过将代表音符的对象传递给每个simple-note
元素(不知道它是否是使元素互相交互的正确方法)。当我按下notes-app
按钮时,我希望它发生。在这种情况下,如何将对象传递给元素属性?
我的坏。我会检查我的例子,并尽快更新我的问题。看不到你的代码示例,你能解决这个问题吗? – Francesco