如何在Ember.js中控制器中访问动态段? [英] How to access dynamic segment in a controller in Ember.js?

查看:71
本文介绍了如何在Ember.js中控制器中访问动态段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  this.route('client',{path:':id'}) ; 

我可以通过我的路线访问这个:

  model:function(params){
console.log(params.id);
}

如何访问:id 在我的控制器中?

解决方案

这是我在应用程序中的做法。不知道这是否是最好的方法。

  App.IndexRoute = Em.Route.extend({
model: function(params){
this.set('params',params);
},
setupController:function(controller,model){
controller.set('params' ,this.get('params'));
this._super(controller,model);
}
});

或者,您也可以在控制器中的容器上进行查找。但我不认为这是一个好办法。这是一个例子。

  this.get('container')。lookup('router:main')。router.currentHandlerInfos 
.findBy('name','index')。params


I have an example route:

this.route('client', {path: ':id'});

I can access this in my route like this:

model: function(params) {
    console.log(params.id);
}

How do I access the :id in my controller?

解决方案

This is how I do it in my application. Not sure if this is the best approach.

App.IndexRoute = Em.Route.extend({
  model: function(params) {
    this.set('params', params);
  },
  setupController: function(controller, model) {
    controller.set('params', this.get('params'));
    this._super(controller, model);
  }
});

Alternatively you can also do a lookup on the container inside your controller. But I dont really think this is a good approach. Here is an example.

this.get('container').lookup('router:main').router.currentHandlerInfos
        .findBy('name','index').params

这篇关于如何在Ember.js中控制器中访问动态段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆