Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在meteor中增加辅助变量计数?_Meteor - Fatal编程技术网

如何在meteor中增加辅助变量计数?

如何在meteor中增加辅助变量计数?,meteor,Meteor,我需要知道如何在meteor中增加helper变量计数 例如: <head> <title>hello</title> </head> <body> <h1>Welcome to Meteor!</h1> {{> hello}} </body> <template name="hello"> <button>Click Me</but

我需要知道如何在meteor中增加helper变量计数

例如:

    <head>
  <title>hello</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>

  {{> hello}}
</body>

<template name="hello">

  <button>Click Me</button>

  {{#each arr}}
     {{counter}} <!-- How to get the foo index here? --> {{name}}
  {{/each}}
</template>

你好
欢迎来到流星!
{{>你好}
点击我
{{{#每个arr}
{{counter}}{{name}
{{/每个}}
Js代码:

 if (Meteor.isClient) {
  // counter starts at 0


  Template.hello.helpers({
    counter: function () {
      return Session.get('counter');
    }
  });

  Template.hello.helpers({
    arr: function () {
      console.log(Session.get('arrres'));
      return Session.get('arrres');
    }
  });

  Template.hello.events({
    'click button': function () {
      Session.set('counter', 0);

      Meteor.call('arrfun',10, function (error, res) {
        if (!error)
        { arrres = res;
          Session.set('arrres', arrres);
          console.log(res);
        }
        else{}
      } );

    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup

    Meteor.methods({

      arrfun:function arrfun(properties, callback)

      {
        var obj = [];
        for(var i = 0 ; i < 10; i++)
        {
          var obj1 = new Object();
          obj1.name = 'abc'+i;
          obj.push(obj1);
        }
        return obj;
      }
    });
  });
}
if(Meteor.isClient){
//计数器从0开始
Template.hello.helpers({
计数器:函数(){
return Session.get('counter');
}
});
Template.hello.helpers({
arr:函数(){
console.log(Session.get('arrres'));
return Session.get('arrres');
}
});
Template.hello.events({
“单击按钮”:函数(){
会话集('计数器',0);
Meteor.call('arrfun',10,函数(错误,res){
如果(!错误)
{arrres=res;
Session.set('arrres',arrres);
控制台日志(res);
}
else{}
} );
}
});
}
if(Meteor.isServer){
Meteor.startup(函数(){
//启动时在服务器上运行的代码
流星法({
arrfun:函数arrfun(属性,回调)
{
var-obj=[];
对于(变量i=0;i<10;i++)
{
var obj1=新对象();
obj1.name='abc'+i;
obj.推力(obj1);
}
返回obj;
}
});
});
}
上面的“arr”包含对象中存在的名称列表。现在可以迭代“arr”,它将出现名称

现在我们可以像打印abc一样打印姓名了吗 2 xyz,直到“arr”完成

那么,我们如何在名称之前打印从1到“arr”长度的数字呢


所以请告诉我该怎么做。

要解释你的问题有点困难,但我想你想要这个:

Template.hello.events({
    'click button': function(event) {
        Session.set('counter', Math.round(Math.random() * 10));
    }
});

Template.hello.helpers({
    'arr': function() {
        var counter = Session.get('counter');
        var arr = [];
        for(var i = 0; i < counter; i++) {
            arr.push(i + 1);
        }
        return arr;
    }
});
Template.hello.events({
“单击按钮”:函数(事件){
Session.set('counter',Math.round(Math.random()*10));
}
});
Template.hello.helpers({
“arr”:函数(){
var counter=Session.get('counter');
var-arr=[];
对于(变量i=0;i
在您的hello模板中:

<ul>
    {{#each arr}}
        <li>{{this}}</li>
    {{/each}}
</ul>
    {{{#每个arr}
  • {{this}}
  • {{/每个}}

这将在每次单击按钮时生成一个介于0-10之间的随机数,然后模板将按顺序计数到此随机数。

Woah
arr
必须是一个数字吗?为什么不
count
total
?arr不是一个假定的数字arr返回objs,所以objs长度假定为10。那么如何在这个循环中迭代1-10个数字。@Kyll您能提供一个纯HTML示例来说明您想要实现的目标吗?浏览器中的最终结果会是什么样子。@user2344293那么您为什么不能使用
  • ,因为这将为您提供编号。不需要复杂的过度工程。是的,可能是重复的,但似乎有点过头了。我没有发现任何东西可以简单地重复一个区块的
    n
    次。。。除了创建一个虚拟数组并对其进行迭代之外,难道没有其他解决方案吗?空格键没有“重复循环n次”函数。它完全依赖于传递对象的游标或要迭代的数组。因此需要生成具有所需元素数的数组。那么,创建自定义块辅助对象怎么样?一些可以定义一次并且可以在任何地方重用的东西。这对我来说不太合适,因为我将objs返回为'arr',所以我如何将这些计算放在arr中。这就是为什么我使用counter helper,即使我们使用ol,它也会在no之后出现。符号。那个。符号也不需要@IanJones@Kyll也许UI.registerHelper实际上可能很有用,如下所述: