1

Trying to write a ui.router in typescript for the first time. as of now my code looks like this:

class Configuration {
    constructor(private $stateProvider: ng.ui.IStateProvider, 
                private $urlRouterProvider: ng.ui.IUrlRouterProvider) {
        this.init();
    }

    private init(): void {
        this.$stateProvider.state("main", Configuration.defaultState());
        this.$stateProvider.state("login", Configuration.login());
        this.$urlRouterProvider.otherwise('/main');
    }

    private static defaultState(): ng.ui.IState {
        return {
            url: "/main"
            , template: "<h1>hello</h1>"
        }
    }

    private static login(): ng.ui.IState {
        return {
            url: "/login"
            , template: "login"
        }
    }
}

angular.module('smm')
    .config(($stateProvider: ng.ui.IStateProvider, 
             $urlRouterProvider: ng.ui.IUrlRouterProvider) => {
        return new Configuration($stateProvider, $urlRouterProvider)
    });

As much as the compile code looks all right, the router does not seem to work, I have no error in the console nor any route is executed. I guess the problem is silly but I can't seem to find it. Any idea?

1 Answer 1

3

For me is your code working as is. There is a working plunker with your code as is.

This is the index.html

<html ng-app="smm" >

  <head>
    <title>my app</title>
    <style>ul { padding-left: 0; } li { list-style: none; }</style>
    <script data-require="angular.js@*"
            src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js"
    ></script>
    <script data-require="ui-router@*" 
            src="//rawgit.com/angular-ui/ui-router/0.2.15/release/angular-ui-router.js"
    ></script>
    <script src="script.js"></script>
    <script src="Config.js"></script>
  </head> 

  <body>
    <ul>
      <li><a href="#/login">/login</a>
      <li><a href="#/main">/main</a>
    </ul>

    <div ui-view=""></div>
  </body> 
</html>

This is script.js:

var app = angular
  .module('smm', [
    'ui.router' 
  ])

And content of the Config.js could be found here

Check the working example here

Note, with

<html ng-app="smm" ng-strict-di>
...

we would need:

angular.module('smm')
    .config(['$stateProvider', '$urlRouterProvider',
    ($stateProvider, $urlRouterProvider) => {
        return new Configuration($stateProvider, $urlRouterProvider)
    }]);

Check that forked version here, and playground snippets here

Sign up to request clarification or add additional context in comments.

2 Comments

awesome, thanks ... thought it was a stupid error from my side, lack of ui-view on the index .... sorry ... and thanks once again
If that helped, great! Enjoy amazing combination - Angular, Typescript and UI-Router ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.