Reputation: 3977
I'm using ui-router-ng2 in an Angular 2 app. How do I tell the router, from JavaScript/Typescript, to navigate to a state? I'm looking for something like:
export class SomeClass {
constructor(private router: UIRouter) { }
somefunc() {
if (some condition) {
this.router.navigate("/whatever")
} else {
this.router.navigate("/home")
}
}
Upvotes: 1
Views: 1659
Reputation: 18049
I would suggest using dependency injection to get access to the StateService
try this in your angular component
import { Component, OnInit } from "@angular/core";
import { StateService } from "@uirouter/core";
@Component({selector: "app-component-name",})
export class SomeComponent implements OnInit {
constructor(private stateService: StateService) { }
ngOnInit() {}
public someFunction(){
this.stateService.go('your_named_state');
}
}
Upvotes: 0
Reputation: 26
This should do it, using state names rather than urls.
import {UIRouter} from "ui-router-ng2";
export class SomeClass {
constructor (private _uiRouter:UIRouter) {}
somefunc() {
if (some condition) {
this._uiRouter.stateService.go('whatever')
} else {
this._uiRouter.stateService.go('home')
}
}
}
Upvotes: 1