如何与Aurelia重复绑定到对象

How to bind to object in repeat.for with Aurelia

本文关键字:绑定 对象 Aurelia      更新时间:2024-05-07

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

游戏卡.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);
  }
}

游戏卡.html

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

使用自定义元素的视图如下:

<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绑定方法中的console.log语句为gameData打印出未定义的内容,但为console.log('name: ' + this.name)语句打印出正确的游戏名称。我不明白为什么当我绑定到游戏对象的属性时绑定有效,但当我绑定游戏对象本身时却无效。任何帮助都将不胜感激,非常感谢!

您必须编写game-data.bind而不是gameData.bind。从第一眼看,这应该是

的唯一问题