我尝试使用Ember进行简单的应用程序。索引控制器:
App.IndexController = Ember.ObjectController.extend({
schools: [{name:"Old school"}],
actions:{
add: function(){
var schools = this.get("schools");
schools.push({name: 'New school'});
this.set("schools",schools);
}
}
});
索引模板:
<script type="text/x-handlebars" data-template-name="index">
<button type="button" {{action "add"}}>Add school</button>
<ul>
{{#each school in schools}}
<li>{{school.name}}</li>
{{/each}}
</ul>
</script>
当我在开始申请午餐时,我看到:
当我点击添加按钮时没有任何反应,为什么?
答案 0 :(得分:1)
您需要使用pushObject
才能让ember知道某个值已添加到列表中。之后没有必要设置它。
add: function(){
var schools = this.get("schools");
schools.pushObject({name: 'New school'});
}