我正在玩聚合物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