angular.js - How to get the URL of the current address bar in angular 2
世界只因有你
世界只因有你 2017-05-15 17:09:22
0
7
883

The URL automatically jumps in the background. How does ng2 get the URL in the address bar and the parameters in it?
The answer found online is ng1, and the method is let aid=$location.search().aid;. But $location cannot be found in ng2.

世界只因有你
世界只因有你

reply all(7)
我想大声告诉你

window.location.href
It seems that ng2 method is not needed

淡淡烟草味

angular 2 It’s impossible not to have $location

//1.获取当前完整的url路径  
var absurl = $location.absUrl();  
//http://172.16.0.88:8100/#/homePage?id=10&a=100  
  
//2. 获取当前url路径(当前url#后面的内容,包括参数和哈希值):  
 var url = $location.url();  
 // 结果:/homePage?id=10&a=100  

Angular obtains and modifies the current page URL by injecting $location

黄舟

window.location.href //URL of the current address bar
this.sub = this.route.params.subscribe(params => {

        let 变量 = +params['参数名'];
        });
        //当前地址参数
Ty80

Official example: Location,

    import {Component} from '@angular/core';
    import {Location} from '@angular/common';
    @Component({selector: 'app-component'})
    class AppCmp {
      constructor(location: Location) {
        location.go('/foo');
      }
    }
给我你的怀抱

I also encountered this problem, I really can’t find it, please help me

某草草
import { Component, OnInit, Inject } from '@angular/core';
import {ActivatedRoute, Params} from '@angular/router';

@Component({
    selector: 'demo',
    template: `<p></p>`
})
export class DemoComponent implements OnInit {
    constructor(@Inject(ActivatedRoute) private router: ActivatedRoute) { }

    ngOnInit() {
        this.router.params.subscribe((params: Params) => {
            // params 
        });
    }
}

https://angular.io/docs/ts/la...

漂亮男人

Let’s talk about getting parameters,
import {ActivatedRoute} from '@angular/router';
Officially, you have to use rxjs, but it’s okay to force it like this:
this._activatedRoute.params'value'

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template