简体   繁体   English

离子框架问题中的ui-router

[英]ui-router in ionic framework issue

I'm having trouble getting my routes to match up with my views. 我无法让我的路线与我的观点相匹配。 I have the following routes: 我有以下路线:

.state('tab.account', {
  url: '/account',
  abstract: true,
  views: {
    'tab-account': {
      templateUrl: 'templates/tab-account.html',
      controller: 'AccountController'
    }
  }
})
.state('tab.account.order', {
  url: '/order/:id', 
  views: {
    'order-view': {
      templateUrl: 'templates/order-view.html', 
      controller: 'OrderController'
    }
  }
})

I have this in templates/tab-account.html 我在templates / tab-account.html中有这个

<ion-view title="Account">
<ion-content class="has-header padding">
<h1>Account</h1>

<h2>Your orders</h2>

<div class="card" ng-repeat="booking in bookings">
  <div class="item item-text-wrap">
    <b><a ng-click="viewOrder(booking.id)">{{ booking.carpark.city }}</a></b> on <b>{{ booking.date | date:'dd.mm.yyyy' }}</b>
  </div>
</div>

I have this function in the controller for that view: 我在该视图的控制器中有此功能:

$scope.viewOrder = function(id) {       
    $state.go('tab.account.order', {id:id});
};

Then finally I have this view for the order: 最后我得到了这个订单的观点:

<ion-view ui-view="orderView" title="Order">
<ion-content class="has-header padding">
    <h1>Your Order</h1>
</ion-content>

The url seems to work, I end up with #/tab/account/order/1 but it doesn't load that final view! 网址似乎工作,我最终用#/ tab / account / order / 1但它没有加载最终视图!

Cheers in advance! 提前干杯!

The ui-router configuration will depend on what you're trying to do. ui-router配置将取决于您尝试做什么。

It looks like you want the account view and the order view to be on separate pages, in which case you won't want it to be an abstract state, since from the ui-router wiki : "An abstract state can have child states but can not get activated itself. An 'abstract' state is simply a state that can't be transitioned to." 看起来您希望帐户视图和订单视图位于不同的页面上,在这种情况下,您不希望它是一个抽象状态,因为从ui-router wiki :“抽象状态可以有子状态但是不能自我激活。“抽象”状态只是一种无法转变的状态。“

I'm not entirely sure you even want a child state for the same reason, since "child states will load their templates into their parent's ui-view." 我不完全确定你出于同样的原因甚至想要一个孩子状态,因为“子状态会将他们的模板加载到他们父母的ui视图中。” ( https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views#nested-states--views ). https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views#nested-states--views )。

If this is the case, then you can do something like this: 如果是这种情况,那么你可以这样做:

.state('tab.account', {
  url: '/account',
  templateUrl: 'templates/tab-account.html',
  controller: 'AccountController'
})
.state('tab.order', {
  url: '/account/order/:id', 
  templateUrl: 'templates/order-view.html', 
  controller: 'OrderController'
})

Here's a plunkr: http://plnkr.co/edit/uuEQcxbWchxQyGV7wMlw?p=preview 这是一个傻瓜: http ://plnkr.co/edit/uuEQcxbWchxQyGV7wMlw?p =preview

The views objects have been removed, since you don't really need them unless you have multiple nested views. 视图对象已被删除,因为除非您有多个嵌套视图,否则您根本不需要它们。

If however you wanted the order view on the same page, then you could do something like this: http://plnkr.co/edit/8Tb1uhVDM0HCW8cgPRfL?p=preview 但是,如果您想在同一页面上查看订单视图,那么您可以执行以下操作: http//plnkr.co/edit/8Tb1uhVDM0HCW8cgPRfL?p = preview

<ion-view title="Account">
  <ion-content class="has-header padding">
    <h1>Account</h1>

    <h2>Your orders</h2>

    <div class="card" ng-repeat="booking in bookings">
      <div class="item item-text-wrap">
        <b><a ng-click="viewOrder(booking.id)">{{ booking.carpark.city }}</a></b> on <b>{{ booking.date | date:'dd.mm.yyyy' }}</b>
      </div>
    </div>
    <ui-view></ui-view> <!-- Where the child view will load -->
  </ion-content>
</ion-view>

app.js: app.js:

.state('tab.account', {
  url: '/account',
  templateUrl: 'tab-account.html',
  controller: 'AccountController'
})
.state('tab.account.order', {
  url: '/order/:id', 
  templateUrl: 'order-view.html', 
  controller: 'OrderController'
})

You'll also probably want to remove your ion-content from the order view template, or else you might get some funky double scroll issues. 您也可能想要从订单视图模板中删除您的ion-content ,否则您可能会遇到一些时髦的双滚动问题。

Hope this helps! 希望这可以帮助! Ui-router is pretty confusing, but their wiki has some great documentation and examples to clear things up. Ui-router非常令人困惑,但是他们的wiki有一些很棒的文档和示例来清理它们。

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

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