HTML
<template name="formExample">
<form>
{{#with formData}}
<input type="text" class="example">
{{/with}}
</form>
</template>
JS
Template.formExample.helpers({
'formData': function() {
return Settings.findOne({ collection_id : getCollectionId() })
}
})
Template.formExample.rendered = function(){
// Does not work
console.log( $('.example'), document.querySelectorAll('.example') )
// >> undefined []
// Works (but obviously bad)
setTimeout(function() {
console.log( $('.example') )
}, 1000)
// Does not work
console.log( this.$('.example'), this.find('.example') )
// >> [prevObject: jQuery.fn.init[0], context: li] null
}
如何使用逻辑 block 内的代码有效查询 DOM、init 函数等?我希望 template.$
和 template.find
方法能够工作。
最佳答案
你在哪里设置formData
?
我尝试了你的例子,它似乎有效。
关于javascript - Meteor - 如果标记嵌套在 {{#with}} block 中,则 DOM 查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31888108/