我似乎无法从 Controller 内部获取模型,即使 Controller 似乎设置了模型属性。下列:
export default Ember.ObjectController.extend({
init: function() {
this._super();
console.log(this.get('model'));
console.log(this.model);
console.log(this);
}
}
打印出来:
有任何想法吗?
最佳答案
所以事实证明,当我通过设置断点来检查模型时,它是空的。我假设控制台显示内容,因为一旦内容到达它就会更新模型。
在 init()
该模型无法访问:
init: function() {
this._super();
console.log(this.get('model')); // null
}
任何方法都一样
.on('init')
:onInit: function() {
console.log(this.get('model')); // null
}.on('init'),
但是该模型可用于操作(我假设是因为在调用该操作时该模型已经建立):
someAction: function() {
console.log(this.get('model')); // model object as expected
}
所以回答我的问题,
this.get('model')
可用于从 Controller 访问模型,但不能用于 init()
或 .on('init')
方法。
关于ember.js - 如何从 Controller 访问模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763306/