Meteor: Using If condition to conditionally display templates when user clicks a navigation link

假装没事ソ 提交于 2020-01-17 14:51:46

问题


I have some templates corresponding to different places. I am using a navigation bar which has links to different places(Manali). I want the corresponding template to be displayed when a particular link is being clicked. I tried assigning id to each anchor link and use it inside the #if loop of the main file. Like below.

{{#if equals id 'badrinath'}}

{{> Manali}}  

{{/if}

I created a helper function also for the comparison purpose.

UI.registerHelper('equals', function(a, b) {
  return a == b; 
});

But it isn't working. Can anyone suggest a solution. What property of the link can I capture and use it to display the template accordingly.


回答1:


You sound to be looking for "routing" functionality.

You might be interested in Iron Router or Flow Router.

You can still implement your functionality without router, as it sounds still a simple situation as described. You are probably just lacking some event listeners to set your id variable to the correct value.

Probably something like:

<a data-role="changetemplate" href="targetTemplate">To Target Template</a>
var id = new ReactiveVar(); // add the reactive-var package

Template.myTemplate.helpers({
  id: function () {
    return id.get();
  }
});

Template.myTemplate.events({
  "click a[data-role='changetemplate']": function (event) {
    event.preventDefault();
    id.set(event.currentTarget.href);
  }
});


来源:https://stackoverflow.com/questions/38549511/meteor-using-if-condition-to-conditionally-display-templates-when-user-clicks-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!