【问题标题】:Meteor: Can't display my text流星:无法显示我的文字
【发布时间】:2016-03-31 18:11:02
【问题描述】:

我想使用 Meteor 建立一个网站。这里只是我的html代码的一个例子。

<head>
    <title>version_0.0.4</title>
</head>

<body>
     <h1>Welcome to Meteor!</h1>
     {{> myTemplate}}
</body>

<template name="myTemplate">
    <p id="&firstContent">Test to show my first content</p>
    <p id="&secondContent">Test to show my second content</p>
    <button type="button" id="save">Save content</button>
</template>

现在当有人按下保存内容时,我想在我的数据库中以“&”符号保存所有 id 的 innerHtml,效果很好。

    /* ----- functions to include ----- */
function inputContent(collection, inhalt, alteID){
    var id = collection.insert({
        content: inhalt,
        htmlId: alteID
    });
    return id;
}     

Template.myTemplate.events({
    'click #save': function () {
        $(document).ready(function() {    //Damit der Code erst ausgeführt wird, wenn der DOM geladen ist
            var all = $('*[id^="&"]');      // Das "&" Zeichen ist der Identifier/ in all speichern aller elemente
            console.log(all);
                for (var i = 0; i < all.length; ++i) {
                    var item = all[i];  //Durch all iterieren
                    var idNeuesItem = inputContent(textList, item.innerHTML, item.id);//Enthält ID über Callback
                    console.log("Gespeichert :" + textList.findOne({_id: idNeuesItem}).content);
                } 
            });
        }
  });

在我做完这些之后,我现在手动删除了 innerHtml 代码,这样我的 htmt 文件看起来像这样:

<template name="myTemplate">
<p id="&firstContent"></p>
<p id="&secondContent"></p>
<button type="button" id="save">Save content</button>

现在我希望可以在我的 id 中显示内容,以便用户看起来像以前一样,但内容来自数据库。我已经尝试过不同的版本,但没有一个有效。有人可以帮我吗?对不起,当这是一个nooby问题时,我对流星很陌生...... 感谢您的帮助

【问题讨论】:

    标签: javascript html mongodb meteor meteor-helper


    【解决方案1】:

    您需要编写一个辅助方法来将您存储的 JSON 对象的某些属性发布到您的数据库中。

    你的看起来像这样(写在服务器端)

    Template.myTemplate.helpers({
      item: function () {
        return itemList.findOne({_id: idNeuesItem});  //retrieve item
      }
    });
    

    然后你必须将它放在模板中,如下所示:

     <p id="&firstContent">
      {#with item}
            {{content}}
      {/with]
      </p>
    

    在此处阅读有关使用 Meteor 和空格键的更多信息: https://www.discovermeteor.com/blog/a-guide-to-meteor-templates-data-contexts/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-31
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多