【发布时间】:2015-11-25 00:26:57
【问题描述】:
你好,我有这条路线
#CoffeeScript
Router.route 'tests/:slug/:type/question/add',
name: 'addQuestion'
controller: testsAddQuestionController
我有这个控制器
@testsAddQuestionController = testsQuestionsController.extend
template: 'mcqQuestionForm'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
我想根据 :type 参数的值从控制器中选择模板,我尝试了两种方法:
@testsAddQuestionController = testsQuestionsController.extend
template: if this.params.type is 'mcq' then 'mcqQuestionForm' else 'somethingelse'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
但是用这种方法我得到了错误this.params is undefined
第二种方法
@testsAddQuestionController = testsQuestionsController.extend
template: if Router.current().route.params.type is 'mcq' then 'mcqQuestionForm' else 'somethingelse'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
但是这种方法导致应用程序崩溃,有没有人知道如何访问路由参数以使这个条件成为从控制器中选择模板的条件?
【问题讨论】:
-
您是否尝试过将模板定义为返回模板名称的函数?
-
您好,谢谢!我忘记将模板定义为函数,下面是我的答案,再次感谢!
标签: meteor controller iron-router