我在我的模板中有一个变量,名为someString,我想在我的控制器中进行设置。我得到一个未捕获的错误:断言失败:TypeError:无法读取未定义的属性'set'。Ember:设置模板中的变量来自控制器
如何使控制器知道模板变量(如果这甚至是解决此问题的方法)?
我的模板看起来是这样的:
{{#modal-dialog action="close" title="New Run"}}
<form role="form" {{action "submit" on="submit"}}>
...
{{input value=emailComparisonRunID class="form-control" placeholder="Comparison Run ID (optional)"}}
<label><small>{{someString}}</small></label>
<br>
...
</form>
{{/modal-dialog}}
我的控制器看起来是这样的:
export default Ember.ObjectController.extend({
needs: 'services',
dataVer: '',
watchEmailComparisonRunId: function() {
var comparisonId = this.get('emailComparisonRunID');
if(comparisonId == null) {
comparisonId = 1;
}
var onIdFound = function(id) {
console.log('FOUND');
this.set('someString', 'FOUND');
};
var onIdNotFound = function(reason) {
console.log('Not Found');
this.set('someString', 'NOT FOUND')
};
this.get('store').find('run', comparisonId).then(onIdFound, onIdNotFound);
}.observes('emailComparisonRunID'),
...
您可以通过在'this.set()'行上放置一个断点并检查'this'来调试。当你看到它等于'window'时,这将是一个很好的线索。顺便说一句,打破了功能发现没有找到,这是写代码,更多的人应该遵循的一个干净的方式表示祝贺。 – 2015-10-27 03:02:30