【问题标题】:Routing In AngularJS using ui-router使用 ui-router 在 AngularJS 中进行路由
【发布时间】:2016-08-25 06:31:02
【问题描述】:

我正在使用 ui-router 但它不起作用。这是我的代码:

在我的 index.html 中

   <li> <a ui-sref="day2">Day 2</a> </li>
    <li><a ui-sref="day3">Day 3</a></li>
    <li><a ui-sref="day4">Day 4</a></li>

我的 main.js

    var myModule = angular.module('myApp', ['ngMessages', 'ui.router']);
myModule.config(function($stateProvider, $urlRouterProvider) {
      $urlRouterProvider.otherwise("/day1");

      $stateProvider

        .state('day1', {
          url: '/day1',
          templateUrl: 'index.html'
        })
        .state('day1.example', {
          url: '/theview',
          template: '<h3>I am a routed view</h3>'
        })

      .state('day2', {
        url: '/day2',
        views: {

          // the main template will be placed here (relatively named)
          '': {
            templateUrl: 'day2.html'
          }
        }
      })

      .state('day3', {
        url: '/day3',
        views: {

          // the main template will be placed here (relatively named)
          '': {
            templateUrl: 'day3.html'
          },

          // the child views will be defined here (absolutely named)
          'directives@day3': {
            templateUrl: 'directives.html'
          },

          // for column two, we'll define a separate controller
          'services@day3': {
            templateUrl: 'service.html'
          }
        }

      })

      .state('day4', {
        url: '/day4',
        views: {
          '': {
            templateUrl: 'day3.html'
          }
        }

      });
    });

当我在浏览器中单击第 2 天、第 3 天和第 4 天的链接时,它不起作用。甚至 day1.example 也不起作用,但我在 index.html 中这样称呼它

<div>
  <a ui-sref=".example" class="save button">Example</a>
</div>
<div ui-view> </div>
</div>

我不确定问题是什么。我已经下载了 ui-router 压缩文件,所以这不是问题。它实际上是在浏览器file:///Users/Projects/AngularJs/index.html#/day1中检测day1路由 我正在关注scotch 教程。

我的问题在于templateUrl: 'day2.html,当我将其更改为template: '&lt;h1&gt; Check if it Works &lt;/h1&gt; 时,第 2 天的链接运行良好。

【问题讨论】:

  • 不要将状态模板设置为使用index.html

标签: javascript angularjs routing angular-ui-router


【解决方案1】:

不要为您的 index.html 页面提供任何状态,如果您想添加任何内容,请添加子状态或使用子页面。
试试这个:-

var myModule = angular.module('myApp', ['ngMessages', 'ui.router']);
myModule.config(function($stateProvider, $urlRouterProvider) {
      $urlRouterProvider.otherwise("/day1");

     $stateProvider

        .state('main', {
          url: '',
          abstract: true,
          templateUrl: 'main.html'
        })

        .state('main.day1', {
          url: '/day1',
          templateUrl: 'main.html'
        })
        .state('main.day1.example', {
          url: '/theview',
          template: '<h3>I am a routed view</h3>'
        })

        .state('main.day2', {
          url: '/day2',
          templateUrl: 'day2.html'
        })

       .state('main.day3', {
          url: '/day3',
          templateUrl: 'day3.html'
       })

       .state('main.day4', {
          url: '/day4',
          templateUrl: 'day3.html'        
       });
    });

【讨论】:

  • 直接打开文件会导致templateUrl出现问题。当我加载file:///Users/Projects/AngularJs/index.html 时,它会将网址检测为file:///Users/Projects/AngularJs/index.html#/day1,因此当我单击第2 天的链接时,它会被翻译为不存在的file:///day2。我的解决方案是使用服务器,我通常使用 nginx,所以我运行我的虚拟机,启动了我的 nginx 服务器,现在当我加载 localhost:8080 时,它会在我的主页(第 1 天代码)和第 2 天运行 url localhost:8080/#/day1localhost:8080/#/day2 与第 2 天的代码(day2.html)链接,一切正常。
猜你喜欢
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 2020-03-17
  • 1970-01-01
相关资源
最近更新 更多