找到你要的答案

Q:two-way binding on array does not work

Q:双向绑定数组不工作

I have an array of strings that I render as a list of inputs. Then there is a way to add a new item (new input) into this list.

However when change a text in the inputs and then add a new item, all the changes that were previously made to the old inputs are gone - and also the array of strings in my controller does not change.

Here is a code: http://ember-twiddle.com/653be725890f234931dd

how can I preserve changes that are made to the "old inputs"? I am quite suprised by this behaviour, since in angular this is a pretty common thing to do and it works out of the box... so I guess I must be doing something terribly wrong here :(

I have an array of strings that I render as a list of inputs. Then there is a way to add a new item (new input) into this list.

然而,当更改输入中的文本,然后添加一个新项时,以前对旧输入的所有更改都已消失,并且我控制器中的字符串数组也没有改变。

Here is a code: http://ember-twiddle.com/653be725890f234931dd

how can I preserve changes that are made to the "old inputs"? I am quite suprised by this behaviour, since in angular this is a pretty common thing to do and it works out of the box... so I guess I must be doing something terribly wrong here :(

answer1: 回答1:

I tested a way that solved the problem, but maybe there's a better way, but it can create a function to perform the change in the value index and the report that there was a change in the controller, sorry the English Google translation.

Template:
    {{#each valueList key="@index" as |value index|}}
      <div>{{input value=value change=(action 'updateItem' index)}}</div>
    {{/each}}

Controller:
    actions : {
        updateItem : function( index ) {
            this.get( 'valueList' )[ index ] = event.target.value;
            this.notifyPropertyChange( 'valueList' );
        },
     },

我测试了一种解决问题的方法,但也许有更好的方法,但它可以创建一个函数来执行值索引的改变和报告中有一个控制器的变化,抱歉英文谷歌翻译。

Template:
    {{#each valueList key="@index" as |value index|}}
      <div>{{input value=value change=(action 'updateItem' index)}}</div>
    {{/each}}

Controller:
    actions : {
        updateItem : function( index ) {
            this.get( 'valueList' )[ index ] = event.target.value;
            this.notifyPropertyChange( 'valueList' );
        },
     },
ember.js