angular2-routing

Scroll Top in angular2

可紊 提交于 2019-11-30 08:31:39
I am working on angular2 web application where I need help on the following. My page consists of multiple components. I want to scroll top of the page when user clicks a button. I tried document.body.scrollTop = 0; but this is not working in Chrome. I Tried document.documentElement.scrollTop=0;window.scrollTo(0, 0); but not working import like this, import { Inject} from "@angular/core"; import { DOCUMENT } from '@angular/platform-browser'; In your constructor add this, constructor(@Inject(DOCUMENT) private document: Document) { } Then you can set the scroll anywhere like this, this.document

Angular 2 dynamically change default route

爱⌒轻易说出口 提交于 2019-11-30 08:26:51
问题 I need to allow the users of my application to change the default route when they want: I have a parameter page where they can select the "page" they want to show first when log on. For example for now, they are redirected to Day when they log on, but they want to be able to change that and to be redirected on week or month when they log on if they want. { path: 'planification', component: PlanificationComponent, children: [ { path: '', redirectTo: 'Day', pathMatch: 'full' }, { path: 'day',

Multiple layouts in nested routes in Angular (2+)

天大地大妈咪最大 提交于 2019-11-30 07:39:24
I'm creating a Dashboard like application. I'd like to achieve the following layout plan in Angular (2+): route - name - layout / - home page - full width layout with tables and charts, etc /reports - reports page - same full width layout with more tables, etc /login - login page - no full width layout, just a simple login form at screen center /signup - signup page - no full width layout, just a simple signup form at screen center /messages - emails - full width layout /messages/new - new email - medium layout, not inheriting from full width layout etc... So basically what I'd like to do is

How to apply css class to a component element when it's created by router-outlet?

南楼画角 提交于 2019-11-30 07:03:58
问题 I have DOM that looks something like this: <app> <router-outlet></router-outlet> <project>...</project> </app> where project element is inserted by the router. How do I add a class to this element? 回答1: Assuming you always want the class applied to this component, you can use host in your component metadata: @Component({ selector:'project', host: { class:'classYouWantApplied` } }) Resulting in: <app> <router-outlet></router-outlet> <project class="classYouWantApplied">...</project> </app> 回答2

Angular 2 routing and direct access to a specific route : how to configure Apache?

孤街醉人 提交于 2019-11-30 06:44:53
I developped a small Angular2 test app which, among other features, leverages routing. It works fine as long as the user first accesses the home page, then navigates to sub pages. If the user tries to directly access a subpage, I got a 404 if I do not configure the web server. This is perfectly normal as there is no "real page" corresponding to the route. I tried to add the following configuration in apache 2.2 to handle HTML5 Push State: <Directory /var/www/html/angular2-sens> Options Indexes FollowSymLinks RewriteEngine On RewriteBase /angular2-sens RewriteRule ^index\.html$ - [L]

Angular 2 - how to use the new angular 2.0.0-rc.1 router

為{幸葍}努か 提交于 2019-11-30 06:24:40
问题 I've started to write a new angular 2 project and I found that I installed 2 angular router: "@angular/router": "2.0.0-rc.1" , "@angular/router-deprecated": "2.0.0-rc.1" , I didn't find in the angular site how to use the new router. For example, what component do I need to import. So my questions are: Should I use the router-deprecated ? Is there any good doc for how to use the new router? 回答1: Here is how to use the Angular 2 Router (RC1), compared to the beta (deprecated) one: Routes

How to pass a parameter to routerLink that is somewhere inside the URL?

早过忘川 提交于 2019-11-30 06:13:21
问题 I know I can pass a parameter to routerLink for routes such as /user/:id by writing [routerLink]="['/user', user.id]" but what about routes such as this one: /user/:id/details Is there a way to set this parameter or should I consider a different URL scheme? 回答1: In your particular example you'd do the following routerLink : [routerLink]="['user', user.id, 'details']" To do so in a controller, you can inject Router and use: router.navigate(['user', user.id, 'details']); More info in the

In angular 2 how to preserve query params and add additional query params to route

会有一股神秘感。 提交于 2019-11-30 06:03:35
For example I am on route /cars?type=coupe and I want to navigate to the same endpoint with additional query params (but keeping existing one). I am trying something like this <a [routerLink]="['/cars']" [queryParams]="{model: 'renault'}" preserveQueryParams>Click</a> The initial query params are preserved (type=cars) but added ones (model=renault) are ignored. Is this expected/correct behavior or is some kind of bug? Looks like preserveQueryParams has priority over queryParams? Is there any other smooth solution? In Angular 4+, preserveQueryParams have been deprecated in favor of

“this” cannot be used in typescript function (Angular)

眉间皱痕 提交于 2019-11-30 06:03:10
问题 I want to reference the keyword "this" in a typescript class in my Angular project. But it cannot be used. I always get the error that the variable I want to change is not defined. Here is my implementation: export class ContactComponent implements OnInit { contactForm: FormGroup; errorMsg:string = ''; redirect = ""; loggedIn(): void { this.redirect = "dashboard"; console.log("success"); in my HTML the redirect variable is connected to a routerLink like this: <a [routerLink]="redirect"></a> I

Angular 2 Jasmine How to test a function of a component

半城伤御伤魂 提交于 2019-11-30 05:14:28
问题 How do I mock the click event of a function in Angular 2? For my Home Component I have: Home Component import { Component } from '@angular/core'; import { Router } from '@angular/router'; @Component({ moduleId: module.id, templateUrl: 'home.component.html', styleUrls: ['home.component.css'], selector: 'home', }) export class HomeComponent { constructor(private router: Router) { } redirectToUpload() { this.router.navigate(['/upload']); } redirectToAbout() { this.router.navigate(['/about']); }