1

Meteor noob 在这里,试图掌握模板的窍门。所以我在JS中有这个设置:

Session.set('activeSection', 'start');

Template.sectionContainer.helpers = ({
    "isActive": function() {
        return (Session.get("activeSection") === 'start') ? "active" : "nope";
    }
});

然后有:

<template name="sectionContainer">
    <section class="{{isActive}}"></section>
</template>

但是,我没有得到预期的活动/否课程。sectionContainer嵌套在另一个模板中,如果这很重要的话。我觉得我在这里遗漏了一些非常简单的东西,我做错了什么?

使用不推荐使用的语法它工作得很好:

Template.sectionContainer.isActive = function() {
    return (Session.get("activeSection") === 'start') ? "active" : "nope";
}

即使Template.sectionContainer.helpers.isActive()从控制台运行也会返回正确的值。

4

2 回答 2

1

你应该像这样使用它:

Template.sectionContainer.helpers({
    isActive: function() {
        return Session.equals("activeSection", "start") ? "active" : "nope";
    }
});
于 2015-01-04T00:15:05.407 回答
1

如果这有效:

Template.sectionContainer.isActive = function() {
   return (Session.get("activeSection") === 'start') ? "active" : "nope";
}

然后在新语法中它将是:

Template.sectionContainer.helpers({
    isActive: function() {
        return (Session.get("activeSection") === 'start') ? "active" : "nope";
    }
});
于 2015-01-04T01:06:14.617 回答