angular2-routing

Angular2 Routing - keeping state of component when route changes [duplicate]

泄露秘密 提交于 2019-11-26 18:15:57
问题 This question already has answers here : Angular2 router 2.0.0 not reloading components when same url loaded with different parameters? (3 answers) Closed 2 years ago . I have an application, with views behind routes, I need to be able to continue from the point when the route changed, but after going back, the component is in its initial state. Is there any way how to keep the state of a component? 回答1: update 2 That's now fixed (Angular 2.3) for the new router by https://github.com/angular

In Angular, how do you determine the active route?

寵の児 提交于 2019-11-26 18:11:16
NOTE: There are many different answers here, and most have been valid at one time or another. The fact is that what works has changed a number of times as the Angular team has changed its Router. The Router 3.0 version that will eventually be the router in Angular breaks many of these solutions, but offers a very simple solution of its own. As of RC.3, the preferred solution is to use [routerLinkActive] as shown in this answer . In an Angular application (current in the 2.0.0-beta.0 release as I write this), how do you determine what the currently active route is? I'm working on an app that

Angular 2 Scroll to top on Route Change

落花浮王杯 提交于 2019-11-26 18:09:43
In my Angular 2 app when I scroll down a page and click the link at the bottom of the page, it does change the route and takes me the next page but it doesn't scroll to the top of the page. As a result, if the first page is lengthy and 2nd page has few contents, it gives an impression that the 2nd page lacks the contents. Since the contents are visible only if user scroll to the top of the page. I can scroll the window to the top of the page in ngInit of the component but, is there any better solution that can automatically handle all routes in my app? You can register a route change listener

angular 2 disable url encoding

梦想的初衷 提交于 2019-11-26 17:52:54
I would like to disable url encoding. When I use this below. this.router.navigate(['/profile', { tags: 'one,two' }]); The url is like this http://localhost:4200/profile;tags=one%2Ctwo I would like it to be pretty like below http://localhost:4200/profile;tags=one,two Is there a way to disable the url encoding? Angular2 by default uses encodeURIComponent() to encode queryParams in URL, you can avoid it by writing custom URL serializer and override default functionality. In my case, I wanted to avoid Angular2 to avoid replacing comma(,) by (%2). I was passing Query as lang=en-us,en-uk where it

Get route query params

谁说胖子不能爱 提交于 2019-11-26 17:47:28
问题 I am trying to migrate from rc1 to rc4 and i have trouble getting query string parameters. ActivatedRoute object always empty. hero.component.ts import {Component, OnInit} from "@angular/core"; import {Control} from "@angular/common"; import {ROUTER_DIRECTIVES, ActivatedRoute} from '@angular/router'; @Component({ template: '../partials/main.html', directives: [ROUTER_DIRECTIVES] }) export class HeroComponent implements OnInit { constructor(private _activatedRoute: activatedRoute) { } ngOnInit

Angular2 canActivate() calling async function

做~自己de王妃 提交于 2019-11-26 17:42:24
I am trying to use Angular2 router guards to restrict access to some pages in my app. I am using Firebase Authentication. In order to check if a user is logged in with Firebase, I have to call .subscribe() on the FirebaseAuth object with a callback. This is the code for the guard: import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; import { AngularFireAuth } from "angularfire2/angularfire2"; import { Injectable } from "@angular/core"; import { Observable } from "rxjs/Rx"; @Injectable() export class AuthGuard implements CanActivate { constructor

Angular2 - Interaction between components using a service

不打扰是莪最后的温柔 提交于 2019-11-26 17:34:11
问题 I have two component A and B, where component A contains a button. I wish when user click on this button, fire a function on component B <A></A> <router-outlet></router-outlet> And the component B is rendered using routing.I am considering using a service with an observable boolean that indicate if the button in A is clicked. Is this the right way to achieve it ? 回答1: Shared service is a common way of communication between non-related components. Your components need to use a single instance

Angular 2 unit testing components with routerLink

守給你的承諾、 提交于 2019-11-26 17:31:14
I am trying to test my component with angular 2 final, but I get an error because the component uses the routerLink directive. I get the following error: Can't bind to 'routerLink' since it isn't a known property of 'a'. This is the relevant code of the ListComponent template <a *ngFor="let item of data.list" class="box" routerLink="/settings/{{collectionName}}/edit/{{item._id}}"> And here is my test. import { TestBed } from '@angular/core/testing'; import { ListComponent } from './list.component'; import { defaultData, collectionName } from '../../config'; import { initialState } from '../..

Share data between components using a service in Angular2

喜欢而已 提交于 2019-11-26 16:16:01
问题 I am developing an application using angular2. I have a scenario where I need to pass complex data (array of objects) from one component to another component(they are not parent-child, they are two separate components) while routing(using router.navigate()). I have googled this and most of the results describe the scenario of components that are parent-child. I have gone through the results and found these ways to pass the data. 1) Create Shared Service 2) Pass as route parameters 2nd

How do I navigate to a sibling route?

匆匆过客 提交于 2019-11-26 15:51:57
问题 Let's presume I got this router config export const EmployeeRoutes = [ { path: 'sales', component: SalesComponent }, { path: 'contacts', component: ContactsComponent } ]; and have navigated to the SalesComponent via this URL /department/7/employees/45/sales Now I'd like to go to contacts , but as I don't have all the parameters for an absolute route (e.g. the department ID, 7 in the above example) I'd prefer to get there using a relative link, e.g. [routerLink]="['../contacts']" or this