javascript - 如何使用 Aurelia 绑定(bind)到 repeat.for 中的对象

标签 javascript aurelia

我目前正在尝试创建一个具有可绑定(bind)属性的自定义元素,并将该属性绑定(bind)到 repeat.for 循环的变量。这看起来应该是一个简单的任务,但我无法让它工作,我想知道它是否与变量是一个对象有关。我的自定义元素的代码如下:

game-card.js

import { bindable } from 'aurelia-framework';

export class GameCard {
  @bindable gameData = null;
  @bindable name = '';

  bind() {
    console.log('card-game-data: ' + JSON.stringify(this.gameData, null, 2));
    console.log('name: ' + this.name);
  }
}

game-card.html

<template>
  <div class="card medium">
    <h3 class="center-align left">${gameData.name}</h3>
    <div class="right-align right">${gameData.isPublic}</div>
  </div>
</template>

使用自定义元素的 View 如下:

<template>
  <require from="./game-card"></require>
  <div>
    <div class="row">
      <div repeat.for="game of games">
        <game-card class="col s6 m4 l3" gameData.bind="game" name.bind="game.name"></game-card>
      </div>
    </div>
  </div>
</template>

游戏对象如下所示:

[{name: 'SomeName', isPublic: true}, {name: 'AnotherName', isPublic: false}]

现在,当我运行代码时,game-card.js 绑定(bind)方法中的 console.log 语句为 gameData 打印出 undefined,但为 console.log( 'name: ' + this.name) 语句。当我绑定(bind)到游戏对象的属性时,我无法弄清楚为什么绑定(bind)有效,但当我绑定(bind)到游戏对象本身时却不起作用。任何帮助将不胜感激,非常感谢!

最佳答案

您必须编写 game-data.bind 而不是 gameData.bind。乍一看,这应该是唯一的问题

关于javascript - 如何使用 Aurelia 绑定(bind)到 repeat.for 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36556688/

相关文章:

javascript - 如何在 WordPress 中的文本输入输入字段中添加自动完成功能

javascript - 循环遍历javascript中的图像数组

javascript - 与奥蕾莉亚的传承

aurelia - 从信号计算?

typescript - 解决VS 2017中的 'Conflicting definitions for node' TS4090错误

javascript - 主干更新嵌套属性

javascript - 复选框值的嵌套循环,与输入表单和以 json 格式保存的表单值进行比较。以及如何避免复选框组中的重复项

javascript - 使用 jQuery 添加位于我的项目中的图像

自定义元素内部的 Aurelia 自定义元素和共享变量

javascript - Aurelia 绑定(bind)在样式复选框上