无法从州''解决'...'

Sef*_*efa 97 angularjs angular-ui-router

这是我第一次尝试使用ui-router.

这是我的app.js

angular.module('myApp', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider){
  $urlRouterProvider.otherwise("/index.html");

  $stateProvider.state('index', {
    url: '/'
    template: "index.html",
    controller: 'loginCtrl'
  });


  $stateProvider.state('register', {
    url: "/register"
    template: "register.html",
    controller: 'registerCtrl'
  });
})
Run Code Online (Sandbox Code Playgroud)

如你所见,我有两个州.我正在尝试注册这样的状态:

<a ui-sref="register">
  <button class="button button-balanced">
    Create an account
  </button>
</a>
Run Code Online (Sandbox Code Playgroud)

但我得到了

无法从州''解决'注册'

例外.这里有什么问题?

Rad*_*ler 68

这种错误通常意味着,(js)代码的某些部分未加载.内部的状态ui-sref缺失.

一个工作的例子

我不是离子的专家,所以这个例子应该表明它会起作用,但是我使用了一些技巧(标签的父级)

这是一个有点调整的状态def

.config(function($stateProvider, $urlRouterProvider){
  $urlRouterProvider.otherwise("/index.html");

    $stateProvider

    .state('app', {
      abstract: true,
      templateUrl: "tpl.menu.html",
    })

  $stateProvider.state('index', {
    url: '/',
    templateUrl: "tpl.index.html",
    parent: "app",
  });


  $stateProvider.state('register', {
    url: "/register",
    templateUrl: "tpl.register.html",
    parent: "app",
  });

  $urlRouterProvider.otherwise('/');
}) 
Run Code Online (Sandbox Code Playgroud)

在这里,我们有父视图,其中包含选项卡及其内容:

<ion-tabs class="tabs-icon-top">

  <ion-tab title="Index" icon="icon ion-home" ui-sref="index">
    <ion-nav-view name=""></ion-nav-view>
  </ion-tab>

  <ion-tab title="Register" icon="icon ion-person" ui-sref="register">
    <ion-nav-view name=""></ion-nav-view>
  </ion-tab>

</ion-tabs>
Run Code Online (Sandbox Code Playgroud)

更多的是一个例子,如何让它运行,然后以正确的方式使用离子框架... 在这里检查这个例子

下面是一个类似的问答用一个例子,使用命名视图(肯定是更好的解决方案) 离子路由问题,显示空白页面

选项卡中具有命名视图的改进版本位于:http://plnkr.co/edit/Mj0rUxjLOXhHIelt249K?p = preview

  <ion-tab title="Index" icon="icon ion-home" ui-sref="index">
    <ion-nav-view name="index"></ion-nav-view>
  </ion-tab>

  <ion-tab title="Register" icon="icon ion-person" ui-sref="register">
    <ion-nav-view name="register"></ion-nav-view>
  </ion-tab>
Run Code Online (Sandbox Code Playgroud)

定位命名视图:

  $stateProvider.state('index', {
    url: '/',
    views: { "index" : { templateUrl: "tpl.index.html" } },
    parent: "app",
  });


  $stateProvider.state('register', {
    url: "/register",
    views: { "register" : { templateUrl: "tpl.register.html", } },
    parent: "app",
  });
Run Code Online (Sandbox Code Playgroud)

  • 如果我很想知道注册了什么状态,我通常会向.run()方法注入$ state信息.然后你可以执行console.log($ states.get()),它将返回一个状态数组.`.run($ state){console.log($ states.get()); });` (4认同)

Mag*_*gus 37

刚来这里分享我发生的事情.
您不需要指定父级,状态以面向文档的方式工作,因此,您可以将状态更改为app.index,而不是指定parent:app

.config(function($stateProvider, $urlRouterProvider){
    $urlRouterProvider.otherwise("/index.html");

$stateProvider.state('app', {
  abstract: true,
  templateUrl: "tpl.menu.html"
});

$stateProvider.state('app.index', {
    url: '/',
    templateUrl: "tpl.index.html"
});

$stateProvider.state('app.register', {
    url: "/register",
    templateUrl: "tpl.register.html"
});
Run Code Online (Sandbox Code Playgroud)

编辑警告,如果你想深入嵌套,我必须指定完整的路径.例如,你不能拥有像这样的状态

app.cruds.posts.create
Run Code Online (Sandbox Code Playgroud)

没有

app
app.cruds
app.cruds.posts
Run Code Online (Sandbox Code Playgroud)

或者角度会抛出异常,说它无法弄清楚溃败.要解决这个问题,您可以定义抽象状态

.state('app', {
     url: "/app",
     abstract: true
})
.state('app.cruds', {
     url: "/app/cruds",
     abstract: true
})
.state('app/cruds/posts', {
     url: "/app/cruds/posts",
     abstract: true
})
Run Code Online (Sandbox Code Playgroud)

  • 感谢`编辑警告,如果你想深入嵌套,我必须指定完整的路径.例如,你不能拥有像`这样的状态,这是我的问题. (9认同)

Gre*_*inn 7

我刚刚和Ionic有同样的问题.

事实证明我的代码没有任何问题,我只需要退出离子服务会话并再次运行离子服务.

回到应用程序后,我的州工作得很好.

如果您正在运行gulp,我还建议您在app.js文件上按几次保存,以确保所有内容都重新编译.