简体   繁体   English

如何使用路由获取:id参数进入angular2服务

[英]how to get :id param into the service of angular2 using routes

I'm looking for a way to pass mac id into service using routes to filter data. 我正在寻找一种使用路由过滤数据将mac id传递到服务的方法。 In my app.html i have a dropdown menu, in which i have a list of mac address displayed(manually written).Using angular activated routes how to get the mac id into the angular services so that i can use it in a filter method. 在我的app.html中,我有一个下拉菜单,其中显示了一个mac地址列表(手动编写)。使用有角激活的路由如何将mac id放入有角服务中,以便可以在过滤器方法中使用它。

[app.html] [app.html]

<ul class="dropdown-menu scrollable-menu" role="menu">

      <li><a routerLink="mac/:id">3s-ds-23-sf-23-ce-33</a></li>
      <li><a routerLink="mac/:id">3s-ds-23-sf-23-ce-34</a></li>
      <li><a routerLink="mac/:id">3s-ds-23-sf-23-ce-35</a></li> 

</ul>

service.ts service.ts

@Injectable()
export class TabuleService {

  //private headers = new Headers({'Content-Type': 'application/json'});
  private _Url = 'http://localhost:3000';  // URL to web api

  constructor(
     private _http: Http,
     private route: ActivatedRoute ) {}



getTemperature(): Observable<testing\[\]> {
    const url = this._Url+'/temperature';                    
    return this._http.get(url)
               .map(res =>{
                let data=res.json();
                let parsedData = \[\]; 
 // here i need to get parameter of mac from the routes to filter
                data.filter(function(el){ return el.mac=="3s-ds-23-sf-23-ce-35" }) 

                .forEach(function(item){ parsedData.push({ 
                          x:item.epoch_time_stamp, y:parseFloat(item.temp) });  });
                          //console.log(parsedData);
                return parsedData; })
               .catch(this.handleError);
               } 

在此处输入图片说明

<ul class="dropdown-menu  scrollable-menu" role="menu">

      <li *ngFor="address in macAddress>
             <a  [routerLink]="['/mac', address]">
                 {{address}}
             </a>
     </li>
</ul>

In your component, 在您的组件中

macAddress:Array<string>=['3s-ds-23-sf-23-ce-33', '3s-ds-23-sf-23-ce-34', '3s-ds-23-sf-23-ce-35';

You cannot get the parameter using 您无法使用获取参数

 constructor(
     private _http: Http,
     private route: ActivatedRoute ) {
           this.mac= this.route.params.subscribe(params => {
                   this.category = + params['address'];


    });
 }

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

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