javascript - 嵌套 View 入门

标签 javascript angularjs angular-ui-router

我完全不知道我设置嵌套 View 的方式有什么问题。我做错了什么???

app.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
  $locationProvider.html5Mode(true);

  $stateProvider
    .state('main', {
      url: '/',
      template: '<div><h1>Hello World</h1><div ui-view></div></div>',
      controller: 'MainCtrl'
    })
    .state('main.test', {
      template: '<div><h2>I\'m here!</h2></div>',
      url: '/here'
    });
});

发生的情况是我转到/,它向我显示“Hello World”,然后我转到/here,它向我显示一个空白页面。我不确定我做错了什么......

最佳答案

url中需要省略前面的/:

.state('main.test', {
  template: '<div><h2>I\'m here!</h2></div>',
  url: 'here'
 });

调试

单击此 anchor 时,ui-router 导航到此位置:http://localhost:8000//here

 <a ui-sref="main.test">test</a>

在您的应用程序中,您可以轻松检查某个州的网址

app.run(function($state){
   var href = $state.href('main.test');
   console.log(href); # '//here'   
});

请务必将服务器设置为支持html5模式

$location documantation

Server side

Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html)

关于javascript - 嵌套 View 入门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20987821/

相关文章:

javascript - 单击链接时如何禁用淡入/淡出 div

javascript - 如何在javascript中隐藏错误框验证

javascript - angular.js 将函数注入(inject)工厂

javascript - Angular ui-router - 如何访问从父模板传递的嵌套命名 View 中的参数?

JavaScript - 改进根据单个条件返回值的递归循环

javascript - 如何使用类获取元素内的元素值

angularjs - 如何在不更改所有单元测试的情况下避免在 app.run 周期期间进行的调用出现 "Unexpected request: GET"

javascript - 通过数组迭代设置 $scope 属性值

javascript - 如何配置 Angular 2 路由器从/src 子文件夹运行

javascript - SPA 在在线托管中不起作用