ecmascript-6 - 在 Aurelia 中将数据从一个 View 模型传递到另一个 View 模型

标签 ecmascript-6 aurelia

我有一个名为 entry-main 的页面它包括这个模板:

<template>
    <entry-search></entry-search>
    <entry-details></entry-details>
</template>

内部 <entry-search>还有另一个自定义元素。它看起来像这样:
<template>
    <div class="row">
        <div class="col-lg-12">
            <div class="input-group">
                <span id="entry_search" class="input-group-addon">
                    <span class="fa fa-search"></span>
                </span>
                <!-- important part -->
                    <typeahead id="choose" placeholder="Holding the place"></typeahead>
                <!-- end of important part -->
            </div>
        </div>
    </div>
</template>

在 typeahead View 模型中,我得到了 typeahead 的值,如下所示:
$(this.id).on('typeahead:selected', function (e, item) {
       this.selected = item;
});

我现在的问题是,我怎样才能得到 this.selected从我的条目详细信息 View 模型中的预输入 View 模型?

为了清楚起见,它应该是这样的:
<entry-main>
    <entry-search>
          <typeahead></typeahead>
    </entry-search>

    <entry-details>
          ${selected}
    </entry-details>
</entry-main>

最佳答案

你可以这样做:

entry-main 中创建属性“已选择” :

export class EntryMain {
    selected = '';
    //rest of the code
}

typeahead 中创建可绑定(bind)属性:
import { bindable } from 'aurelia-framework';

export class Typeahead {
    @bindable selected;
    //... rest of the code
}

将 typeahead 的“selected”绑定(bind)到 entry-main 的“selected”
<entry-main>
    <entry-search>
        <typeahead selected.bind="selected"></typeahead>
    </entry-search>

    <entry-details>
        ${selected}
    </entry-details>
</entry-main>

未经测试的代码,但我认为它应该可以工作。

关于ecmascript-6 - 在 Aurelia 中将数据从一个 View 模型传递到另一个 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34416889/

相关文章:

javascript - Aurelia: Uncaught Error :doSomething 不是函数

javascript - 为什么 babel 将导入的函数调用重写为 (0, fn)(...) ?

javascript - 在 Babel 中导出 es6 类 - 找不到模块

javascript - 如何在 aurelia 自定义组件中获取路由参数

typescript - 如何在具有 getter 和 setter 的属性上使用 Aurelia 的可绑定(bind)装饰器

Aurelia HttpClient 取消请求

javascript - 在 Web 应用程序中访问电话库

javascript - 即使我在构造函数中抛出错误,也不会触发 componentDidCatch

javascript - 将使用 range 的 Coffeescript 循环转换为 ES6

javascript - 无法弄清楚mapreduce算法