简体   繁体   English

Ember.js如何在视图中显示动态段?

[英]Ember.js How to show the dynamic segment in the view?

In my routes, I have this: 在我的路线中,我有这个:

App.Router.map(function() {
    this.resource('posts', function() {
        this.route('search', {path: 'search/:query'});
    });
});

And in my search route, I have this: 在我的搜索路线中,我有以下内容:

App.PostsSearchRoute = Ember.Route.extend({
    model: function(params) {
        return Em.$.getJSON('posts?q=' + params.query);
    }
});

So when I go to the url: /posts/search/posttobesearched , params.query becomes posttobesearched . 因此,当我转到url: /posts/search/posttobesearchedparams.query变为posttobesearched

Now, in my search template, I would like posttobesearched to be shown. 现在,在我的搜索模板中,我希望显示posttobesearched I did it like this: 我这样做是这样的:

<h2>Search Results for: <em>{{query}}</h2>

But it didn't show the query . 但是它没有显示query

Please tell me how to do this. 请告诉我该怎么做。

I think your controller needs to have this on it: 我认为您的控制器需要具备以下功能:

  queryParams: ['query'],
  query: null

and then I think that your {{query}} in a template should work at that point. 然后我认为模板中的{{query}}应该可以正常工作了。

[ edit ] Also, note that you have to have the query params feature on: query-params-new. [edit]另外,请注意,您必须在以下位置启用查询参数功能:query-params-new。 You can read more about this in http://emberjs.com/guides/routing/query-params/ which I'm guessing you probably have already read. 您可以在http://emberjs.com/guides/routing/query-params/中阅读有关此内容的更多信息,我想您可能已经阅读过。

Note also that I think what you probably want is actually for the query param to be q, given that's what you have in your API. 还要注意,我认为您可能想要的实际上是将查询参数设置为q,因为这就是您的API中所拥有的。 You really should re-read the linked piece and follow that. 您确实应该重新阅读链接的文章,然后再进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM