我創建了plunkr來說明我遇到的問題。綁定到奧裏利亞的模板部件中的自定義元素
我正在創建一個儀表板,它目前包含四個不同的項目。我將這些項目創建爲自定義元素,然後將它們封裝在名爲Widget的自定義元素中,以爲它們提供框架,標題和樣式。下面是摘錄的樣子:
<widget title="A Widget" icon="fa-question">
<template replace-part="item-template">
<child-element text.bind="$parent.$parent.someText"></child-element>
</template>
</widget>
僅供參考窗口小部件視圖看起來是這樣的:
<template>
<require from="./widget.css!"></require>
<div class="widget">
<div class="widget-header">
<i class="fa ${icon}"></i>
<h3>${title}</h3>
</div>
<div class="widget-content">
<template replaceable part="item-template"></template>
</div>
</div>
</template>
和視圖模型是:
import {bindable} from "aurelia-framework";
export class WidgetCustomElement {
@bindable title;
@bindable icon;
@bindable show; // This is something I want the child element
// to be able to bind to and control but haven't
// got there yet!!
}
但是請注意,我想將來自ViewModel的數據綁定到子元素中,子元素如下所示:
import {bindable} from "aurelia-framework";
export class ChildElementCustomElement {
@bindable text;
}
和視圖:
<template>
<p>The widget passed us : ${text}</p>
</template>
的問題是,不管是什麼表情我使用(在這裏我目前正試圖$parent.$parent.someText
)我不能綁定工作。
應該這樣工作嗎?我也試着定義主視圖模型爲'@Bindable someText」變量someText
但拋出以下異常:
Unhandled promise rejection TypeError: Cannot read property 'some-text' of null
at BindableProperty.initialize (https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.4.0/jspm_packages/github/aurelia/[email protected]/aurelia-templating.js:2448:33)
at new BehaviorInstance (https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.4.0/jspm_packages/github/aurelia/[email protected]/aurelia-templating.js:2199:23)
at HtmlBehaviorResource.create (https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.4.0/jspm_packages/github/aurelia/[email protected]/aurelia-templating.js:2821:30)
at https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.4.0/jspm_packages/github/aurelia/[email protected]/aurelia-templating.js:3385:27
at f (https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.4.0/jspm_packages/npm/[email protected]/client/shim.min.js:1415:56)
at https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.4.0/jspm_packages/npm/[email protected]/client/shim.min.js:1423:13
at b.exports (https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.4.0/jspm_packages/npm/[email protected]/client/shim.min.js:453:24)
at b.(anonymous function) (https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.4.0/jspm_packages/npm/[email protected]/client/shim.min.js:1625:11)
at Number.f (https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.4.0/jspm_packages/npm/[email protected]/client/shim.min.js:1596:24)
at q (https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.4.0/jspm_packages/npm/[email protected]/client/shim.min.js:1600:11)
感謝您輸入@sayan。這不完全是我想要的,但有一個稍微不同的原因。我試圖維護這個小部件,它能帶來外觀和感覺,但不會影響任何業務功能。換句話說,我希望子元素跳過一代,並綁定到更高的上下文,在我的應用程序中是一個儀表板 - 儀表板爲子元素提供應顯示信息的上下文。到目前爲止,我的最佳想法是使用DI在儀表板和子元素之間注入共享服務(儀表板讀取和寫入,而子元素只讀) – Phil