我正在努力访问一个对象:在控制器中我有一个函数数组,function1接收一个对象,如何从当前模板的这个控制器访问这个对象?
function1中的“this”范围是functionsArray,因此它只有function1和function2方法,因此无法在function1中设置这样的控制器变量:this.set('data',data_to_pass) ; - 它说“this.set不是函数”。
我从另一个组件接收data_to_pass,因此一个解决方案是将此控制器传递给组件,然后在function1中将其传回,但从性能的角度来看,这将是一个糟糕的决定。
export default Ember.Controller.extend({
t: [], // not visible inside functionsArray
functionsArray: {
function1(data_to_pass) {
// how would I pass data_to_pass to this controller or template here?
this.set('data', data_to_pass); // doesn't work, scope of "this" is functionsArray
console.log('data' + data_to_pass.fullName);
},
function2() {
},
}
});
答案 0 :(得分:0)
试试这个:
t: [], // not visible inside functionsArray
functionsArray: Ember.Object.create({
function1: function(data_to_pass) {
// how would I pass data_to_pass to this controller or template here?
this.set('data', data_to_pass); // doesn't work, scope of "this" is functionsArray
alert(this.get('data'));
},
function2: function() {
},
}),
actions: {
press: function() {
this.functionsArray.function1('works!');
}
},
<script type="text/x-handlebars" data-template-name="index">
<button {{action "press"}}>Press here</button>
</script>