2017-02-27 15 views
0

我有一个关于在角2中动态追加url params的问题。角度1使用$ location.search在url中添加查询参数。我怎么能在Angular 2中实现相同的效果?如何在angular 2类型脚本中添加带参数的url?

//Api call to submit product details 
submit_product() { 
       let model = new AddProductModel(this.post_dict.name, this.post_dict.slug, 'N', 0, this.post_dict.display_sitewide, this.post_dict.display_in_party, JSON.parse(localStorage.getItem('bundled')), 'N', parseInt(JSON.parse(localStorage.getItem('categories')))); 
       this 
         .product 
         .addProduct(model) 
         .then(productObj => { 
           if (productObj) { 
             localStorage.removeItem('categories'); 
             // Here i wish to set the object id as the URL parameter; 
             this.successMessage = 'Product Information added Successfully'; 
             this.timeoutMethod(true); 
           } 
         }) 
         .catch(error => { 
           this.errorMessage = error.error.message; 
           this.timeoutMethod(false); 
         }) 

     } 

任何帮助将非常感谢!谢谢一吨。

+0

'使用this._router.navigate(['/ booking',{id:'keeping'}])'其中'/ booking'是路径和'{id:'保留;}'是您希望的id通过 – Smit

+0

你提供的代码甚至与你的问题有关吗?我在哪里看不到网址? *带参数*的url,你的意思是一个http调用或路由吗? :) – Alex

+0

@Smit谢谢吨tonnn它真的帮助! –

回答

1

.ts

使用this._router.navigate(['/booking',{ id: 'keeping'}])其中/booking是路径和{id: 'keeping;}

.html

使用[routerLink]="['/booking',{ service: 'keeping'}]"其中/booking是路径和{id: 'keeping;}

<a [routerLink]="['/booking',{ service: 'keeping'}]">Book</a> 

希望这有助于。

相关问题