javascript 似乎被调用了,因为我看到 console.logs 被触发但没有显示实际的 HTML
通常我会把它作为 Controller 中的一个 Action ,但由于我希望 javascript 在页面本身加载时加载,所以我使用 willInsertElement 将它注入(inject)到 View 中。
VpcYeoman.DocView = Ember.View.extend({
toggleLetterSwitch: false,
togglePermitSwitch: false,
templateName: 'doc',
willInsertElement: function() {
var docTemplateID = this.get('docTemplateID');
if ( docTemplateID == 2) {
this.set('toggleLetterSwitch', true);
this.set('togglePermitSwitch', false);
console.log('docTemplateID equals 2');
} else {
this.set('toggleLetterSwitch', false);
this.set('togglePermitSwitch', true);
console.log(this.get('toggleLetterSwitch'));
console.log('docTemplateID else 1');
}
}
});
-- 文档.hbs --
{{#if toggleLetterSwitch}}
<h1>Showing Letter Template</h1>
{{/if}}
{{#if togglePermitSwitch}}
<h1>Showing Permit Template</h1>
{{/if}}
即使未找到 docTemplateID,该函数仍应切换默认模板。
最佳答案
当然,您应该像这样更改您的 ifs:
{{#if view.toggleLetterSwitch}}
问题是 toggleLetterSwitch 存在于您的 View 中,而不存在于 Controller 或模型中。
关于javascript - 我希望我的 Ember.js 条件在切换 bool 值时加载不同的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23740468/