2

我在我的 Aurelia 应用程序中使用自定义元素。当我将视图模型中的数据与自定义元素绑定时,它工作正常。即使我对自定义元素控件中的数据进行了一些更改,由于双向数据绑定,这些更改也会反映到我的视图模型中的数据中。

但是,如果我对视图模型中的数据进行一些更改(使用 javascript),自定义元素中的数据不会更新。我已经复制了这个问题以获得更简单的设置。

简单视图模型

export class Playground {

  public testObj: any;
  counter = 0;


  constructor() {
    this.testObj = {
        prop1: "This is prop1 value"           
        , collection2: [{ id: "item21" }]
    }        
  }

  updateProp1() {
    alert("before update: "+this.testObj.prop1);
    this.testObj.prop1 = "Sayan " + this.counter++;
    alert(this.testObj.prop1);
  }

  verifyChange() {
    alert(this.testObj.prop1);
  }
}

简单视图

<template>
<h1>
    Playground
</h1>

<div >         
    <div repeat.for="item of testObj.collection2">
        <div class="row">
            <div class="col-sm-4">
                <input type="text" class="form-control" placeholder="prop1"
                       value.bind="$parent.testObj['prop1']">
            </div>
        </div>
    </div>

    <button click.delegate="updateProp1()" class="btn btn-primary"> Update Prop1 </button>
    <button click.delegate="verifyChange()" class="btn btn-primary"> Verify Change </button>

</div>

</template>

现在,每当我Verify Change在更改文本框中的值后单击时,更改的值都会出现在警报中。但是,如果我单击Update Prop1按钮,prop1值会更新,但更改不会反映在视图中。我不确定我到底错过了什么。

注意:数据绑定可以正常工作,而不是使用$parent.testObj['prop1'], if 。$parent.testObj.prop1但是,我的实际自定义元素是通用类型的,并且事先不知道属性名称,因此我似乎需要继续使用$parent.testObj['prop1']符号而不是点符号:$parent.testObj.prop1

在指针/建议/反馈表示赞赏。

更新:如果有人能指出点符号和索引器符号wrt aurelia 数据绑定之间的区别(我已经检查过),那将有很大帮助。

4

1 回答 1

3

这是该aurelia/binding模块早期版本中的一个问题。以下是相关问题:

我在最新版本的 aurelia 中尝试了您的视图/视图模型,一切正常。这是我看到的截屏视频:http: //screencast.com/t/KqcuqXWjkU2

确保您安装了最新版本的 aurelia 组件 - 在此处更新步骤:http ://blog.durandal.io/2015/05/01/aurelia-may-status-and-releases/

于 2015-05-26T13:03:01.820 回答