简体   繁体   English

将angular-route重构为angular-ui-router

[英]refactor angular-route to angular-ui-router

I've created a single page application using ngRoute like so: 我已经使用ngRoute创建了一个单页应用程序,如下所示:

index.html (ngRoute) index.html(ngRoute)

var smallTalkzModel = angular.module('smallTalkzModel', ['ngRoute']);
smallTalkzModel.config(function($routeProvider) {

    $routeProvider
    .when('/', {
      templateUrl : 'components/login/loginView.html',
      controller  : 'loginController'
    }) 
    .when('/chat', {
      templateUrl : 'components/chat/chatView.html',
      controller  : 'chatController'
    });
  }).run(function ($rootScope) {

    // App is loading, so set isAppLoading to true and start a timer
    console.log($rootScope);
    $rootScope.isAppLoading = true;

  });;

small-talkz.model.js (ngRoute) small-talkz.model.js(ngRoute)

 <div ng-app="smallTalkzModel" >
    <div ng-view>
    </div>

 </div>

Then I heard that it is better to use ui.router because it is a 3rd party which have more capabilities and can does everything that ngRoute does and more and will be supported in future version of angular js... 然后我听说最好使用ui.router,因为它是第三方,它具有更多的功能,可以执行ngRoute所做的一切,并且将在以后的angular js版本中提供更多...

So, I tried to refactore my code into the code below. 因此,我尝试将我的代码重构为以下代码。 which does not work... 这不起作用...

Can anyone tell me why? 谁能告诉我为什么? I did not use ng-surf because I don't have any links into my html. 我没有使用ng-surf,因为我的html中没有任何链接。 I get to them by localhost:3000/somePage and not by navigation... 我通过localhost:3000 / somePage而不是通过导航来到达它们...

index.html (ui.router) index.html(ui.router)

<div ng-app="smallTalkzModel" class="loader">
    <div ui-view>
    </div>
</div>

small-talkz.model.js (ui.router) small-talkz.model.js(ui.router)

var smallTalkzModel = angular.module('smallTalkzModel', ['ui.router']);
smallTalkzModel.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider
  .state('login', {
    url: '/',
    templateUrl : 'components/login/loginView.html',
    controller  : 'loginController'
  }) 
  .state('chat', {
    url: '/chat',
    templateUrl : 'components/chat/chatView.html',
    controller  : 'chatController'
  });
});;

angular-ui-router is based on states. angular-ui-router基于状态。 If you want to go from one state to another then you have to use "ui-sref" or "$state.go()" 如果要从一种状态转到另一种状态,则必须使用“ ui-sref”或“ $ state.go()”

for example to go to your chat page (State), use: 例如,要转到您的聊天页面(州),请使用:

<a ui-sref="chat">to go chat page</a>

Expecting that you have included the library ui-router.js in your project. 期望您在项目中包含了ui-router.js库。 Code looks fine. 代码看起来不错。 One thing i think might me causing issue is to define the route when none of the states defined by you matches the state of application. 我认为可能会引起问题的一件事是,当您定义的状态均不匹配应用程序状态时,定义路由。 Use otherwise option for that as below: 为此使用其他选项:

$urlRouterProvider.otherwise('/');

Code should look like: 代码应如下所示:

smallTalkzModel.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider
  .state('login', {
    url: '/',
    templateUrl : 'components/login/loginView.html',
    controller  : 'loginController'
  }) 
  .state('chat', {
    url: '/chat',
    templateUrl : 'components/chat/chatView.html',
    controller  : 'chatController'
  })
  $urlRouterProvider.otherwise('/');

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM