【问题标题】:Where should I initialize select when using Materialize and Meteor?使用 Materialize 和 Meteor 时应该在哪里初始化选择?
【发布时间】:2015-05-01 06:37:12
【问题描述】:

我正在尝试在 Meteor 上使用 Materialise Forms。在其 Materialize 的页面上,它说我应该像这样初始化“选择”输入字段:

$(document).ready(function() {
  $('select').material_select();
});

我尝试在 Meteor.startup、Template.body.created 上调用它 - 没有任何效果。我收到以下错误:

undefined 不是函数(评估 '$('select').material_select()')

我应该在哪里初始化它?

【问题讨论】:

    标签: javascript jquery meteor materialize


    【解决方案1】:

    Akshat 的回答是正确的,但如果它仍然对您不起作用,则可能存在订阅未触发或未渲染 DOM 的所有需要​​部分的问题。然后使用后冲洗。

    Template.listing.onRendered(function () {
        var template = this;
    
        template.subscribe('listOfThings', function () {
        Tracker.afterFlush(function() {
           template.$('select').material_select();
        });
      });
    });
    

    以下是关于此的对话:https://github.com/meteor/meteor/issues/4401#issuecomment-103340262

    还有文档:http://docs.meteor.com/api/tracker.html#Tracker-flush

    【讨论】:

      【解决方案2】:

      使用模板的.renderedcallback

      <template name="hello">
          <select><option>...</option></select>
      </template>
      

      那么你就可以在你的js文件里有这个了

      Template.hello.onRendered(function() {
          $('select').material_select();
      });
      

      模板最有可能在渲染已经触发后添加到正文中,这就是渲染的正文不起作用的原因。如果您使用.created,则 DOM 尚未渲染。

      【讨论】:

      • That's great, but it fails when the select is rendered dynamically (i.e., not when the template is first loaded, but later).在这种情况下,这段代码必须在 呈现选择之后调用......除非有人有更好的主意。
      猜你喜欢
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 2011-02-12
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多