angularJS中的状态提供者和路由提供者 [英] state provider and route provider in angularJS

查看:138
本文介绍了angularJS中的状态提供者和路由提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的app.js文件

Below is my app.js file

angular
  .module('repoApp', [
    'ngAnimate',
    'ngAria',
    'ngCookies',
    'ngMessages',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.bootstrap',
    'ui.router'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .when('/about', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl'
      })
      .when('/login', {
        templateUrl: 'views/loginPage.html',
        controller: 'loginCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });
angular
  .module('loginState',['ui.router']);

以下是我的州档案

angular
  .module('repoApp')
  .config(function ($stateProvider) {

      $stateProvider.state('home1', {
        url:'/home1',
        templateUrl: 'views/modals/test.html'
      })
      .state('secondState',{
        url:'/secondState',
        templateUrl: 'views/modals/secondStateTest.html'
      });
  });

问题是,使用我的html导航到登录页面。

The problem is, using my html i navigate to login page.

<ul class="nav navbar-nav">
              <li class="active"><a href="#/">Home</a></li>
              <li><a ng-href="#/about">About</a></li>
              <li><a ng-href="#/">Contact</a></li>
              <li class="loginShift"><a ng-href="#/login">Login</a></li>
            </ul>

但我试图在流量达到控制器后立即点击状态

but I am trying to hit the state as soon my flow hit the controller

angular.module('repoApp')
  .controller('loginCtrl', function ($scope,$modal,$state) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
    $state.go('home1');
    $scope.openDialog = function () {
        $modal.open({
          keyboard: 'static',
          templateUrl: 'views/login/loginCred.html',
        });
      };        
  });

但我无法进入本国状态。
如果我更改我的状态文件,即

but I am not able to hit the home state. If I change my states file i.e

$stateProvider.state('home1', {
            url:'/login',
            templateUrl: 'views/modals/test.html'
          })

这里我更改了URL。它现在工作正常。

here I changed URL. It works fine now.

我有一个模板,我想导航到下一个状态

I have a template from where I want to navigate to a next state

<div>
<button data-ng-click="openDialog()">open ME!</button>
<div><a ui-sref="secondState">click here</a></div>
</div

但是一旦我点击这个锚标签它会导航我回到主页。即不到我打算去的州。
主要问题是URL(我猜)任何帮助将不胜感激。

but as soon I click this anchor tag it navigates me back to home page. ie not to the state I intend to go. The main issue is URL(i guess) any help will be appreciated.

推荐答案

你不应该同时使用 ngRoute UI-router 。以下是UI路由器的示例代码:

You shouldn't use both ngRoute and UI-router. Here's a sample code for UI-router:

repoApp.config(function($stateProvider, $urlRouterProvider) {
  
  $stateProvider
    .state('state1', {
      url: "/state1",
      templateUrl: "partials/state1.html",
      controller: 'YourCtrl'
    })
    
    .state('state2', {
      url: "/state2",
      templateUrl: "partials/state2.html",
      controller: 'YourOtherCtrl'
    });
    $urlRouterProvider.otherwise("/state1");
});
//etc.

你可以在这个帖子中找到关于这两者之间差异的一个很好的答案: angular-route和angular-ui-router之间有什么区别?

You can find a great answer on the difference between these two in this thread: What is the difference between angular-route and angular-ui-router?

您也可以咨询U. I-Router的文档在这里: https://github.com/angular-ui/ui-router

You can also consult UI-Router's docs here: https://github.com/angular-ui/ui-router

这篇关于angularJS中的状态提供者和路由提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆