ngUpgraders / ng-forward

The default solution for those that want to write Angular 2.x style code in Angular 1.x
411 stars 36 forks source link

$state and $location undefined ... need programmatic way to change URL for routing #162

Open ghost opened 8 years ago

ghost commented 8 years ago

When a button is pressed, I need to authenticate the user before setting the URL allowing the user to route to the next page. Thus I can't use ui-sref. I tried $location.Path and $state.go and neither is defined.

import { $state } from 'ng-forward'; is undefined... thus subsequent @Inject or $state.go("/homePage"); will fail.

What alternative would you suggest ... hopefully not javascript force a new URL.

Thanks, Chuck Duncan Camp Systems, Merrimack, NH

j-walker23 commented 8 years ago

Sorry if i don't understand your question. But this might be what you are looking for.

import { Inject, Component } from 'ng-forward'
import { IStateService } from 'angular-ui-router'
type ILocationService = angular.ILocationService

/**
 * @author john
 * @version 4/8/116
 */

@Component({
  selector: 'some-comp',
  template: ``
})
@Inject('$state', '$location')
export class SomeComponent {

  constructor (private $state: IStateService, private $location: ILocationService) {

    this.$state.go('some-state')
    // or
    this.$location.url('/homePage')
  }

}