我有一个页面,向用户展示几个问题并提供答案的输入字段。
我还有一个按钮添加问题.
单击此按钮后,他可以在新的输入字段中输入问题,然后单击“保存”。所以我需要的是,当他保存时,新输入的问题也应该显示在已经显示的问题附近。
所以我对模型有疑问
import DS from 'ember-data';
export default DS.Model.extend({
challengeQuestions: DS.attr()
});
然后我在控制器中为其添加别名,并显示模板中的两个问题。
import Ember from "ember";
export default Ember.Controller.extend({
addQuestion : false,
questions : Ember.computed.alias('model.challengeQuestions'),
actions : {
postAnswer: function(){
alert('prepare post data');
},
addQuestion: function(){
this.set('addQuestion', true);
},
saveQuestion: function() {
var que = this.get('newQuestion');
this.get('questions').push(que);
this.send('cancel');
},
cancel: function(){
this.set('addQuestion', false);
}
}
});
下面是我的模板..
{{log questions}}
{{#each question in questions}}
<div>
<span>{{question}} : </span>
{{input placeholder="your answer"}}
</div>
{{/each}}
<br><br>
{{#if addQuestion}}
{{input placeholder="Enter your question" value=newQuestion}}
<br>
<button {{action "saveQuestion"}}>Save</button> <button {{action "cancel"}}>cancel</button>
{{else}}
<button {{action 'addQuestion'}}>Add manual Question</button>
{{/if}}
<br><br>
<button {{action 'postAnswer'}}>Submit</button>
所以我在这里想做的是,当我添加新问题并单击“保存”按钮时,我将输入的问题字符串推送到问题我的控制器中的数组。我预计模板会重新渲染,因为它被修改了。
我可以看到新字符串已成功添加,但它没有显示在用户界面上。知道为什么吗?
我正在使用最新的 ember(1.13) 和 ember-cli。
Thanks