【问题标题】:$state.go in Ionic with ion-nav-view does not work带有 ion-nav-view 的 Ionic 中的 $state.go 不起作用
【发布时间】:2015-02-22 12:54:19
【问题描述】:

我已经启动了一个 Ionic Tabs 项目。现在我有一个按钮“initiateProcess”,我用ng-click 调用它并在控制器中执行一些操作,然后通过state.gostate.transitionTo 进入某个状态(tab.target)完成它。

但是,当我不在 tabs.html 中包含该状态时,将不会加载该状态。当我确实包含它时, state.go 工作正常。此状态是独立的(可以视为子状态,但本身不是),我不希望它显示在我的标签中。

发生了什么事?

tabs.html(不带tab.target

<!--
Create tabs with an icon and label, using the tabs-positive style.
Each tab's child <ion-nav-view> directive will have its own
navigation history that also transitions its views in and out.
-->
<ion-tabs class="tabs-icon-top tabs-color-active-positive">


  <!-- Dashboard Tab -->
  <ion-tab title="Status" icon-off="ion-ios7-pulse" icon-on="ion-ios7-pulse-strong" href="#/tab/dash">
    <ion-nav-view name="tab-dash"></ion-nav-view>
  </ion-tab>

  // when I include here Target Tab with <ion-nav-view name = "tab.target" ... then it works


  <!-- Account Tab -->
  <ion-tab title="Account" icon-off="ion-ios7-gear-outline" icon-on="ion-ios7-gear" href="#/tab/account">
    <ion-nav-view name="tab-account"></ion-nav-view>
  </ion-tab>


</ion-tabs>

app.js

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'ngCordova', 'ionic.utils', 'starter.controllers', 'starter.controllers-cloaking', 'starter.services'])

.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) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: "/tab",
    abstract: true,
    templateUrl: "templates/tabs.html"
  })

  // Each tab has its own nav history stack:
  .state('tab.dash', {
    url: '/dash',
    views: {
      'tab-dash': {
        templateUrl: 'templates/tab-dash.html',
        controller: 'DashCtrl'
      }
    }
  })

  .state('tab.target', {
    url: '/target',
    views: {
      'tab-target': {
        templateUrl: 'templates/tab-target.html',
        controller: 'TargetCtrl'
      }
    }
  })

  .state('tab.account', {
    url: '/account',
    views: {
      'tab-account': {
        templateUrl: 'templates/tab-account.html',
        controller: 'AccountCtrl'
      }
    }
  });

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/tab/dash');

});

DashController 中的函数($state 已注入)

 $scope.initiateProcess = function() {

        // some stuff

        $state.go('tab.target')

    }

tab-dash.html 中的按钮

<button class="button button-balanced" ng-click="initiateProcess()">
     Go to Target
    </button>

【问题讨论】:

  • 你能检查你的控制台看它是否抛出任何错误或警告吗?
  • 没有错误或警告......
  • 检查您的错误。通常有一个列表列出您的文件名及其错误。您可以像这样忽略默认的离子错误:(localhost:8100/lib/ionic/js/ionic.bundle.js:7888:12)。
  • 我没有看到任何错误。问题是只要我在 tabs.html 中包含 ion-nav-view name="tab.target" 它就可以工作
  • 好吧,离子中的标签需要名称。它与您的 app.js 中的视图名称匹配......让我输入正确的格式

标签: javascript angularjs ionic-framework


【解决方案1】:

通过禁用 $urlRouterProvider.otherwise() 检查你的路由器是否真的工作。因为它会自动将您路由回 tab.dash,这似乎是路由器错误。

如果您的 url 确实更改为 tabs/target/,这意味着您的路由器确实可以正常工作。试试这个。

   <ion-tab title="Status" icon-off="ion-ios7-pulse" icon-on="ion-ios7-pulse-strong" href="#/tab/target">
        <ion-nav-view name="tab-target"></ion-nav-view>
      </ion-tab>

.state('tab.target', {
    url: '/target',
   'tab-target': {
      templateUrl: 'templates/tab-target.html',
      controller: 'TargetCtrl'
    }
  })

ion-nav-view 类似于 ios 中的 UIViewController。它是您视图的容器。如果您决定命名它,那么您必须与 $stateProvider 中的视图名称相匹配。基本上 ionic 很困惑你想在哪里显示 tabs.target,因为没有匹配的容器

【讨论】:

  • 如何从控制器重定向到 state tab.target?
  • 尝试检查您的路由器是否真的工作。禁用 $urlRouterProvider( 通过评论它),然后在单击时检查您的 url。您的网址是否更改为“/tab/target”?如果是这样,那么问题出在导航视图
【解决方案2】:

我遇到了类似的问题,我试图转到一个没有标签的视图,就像你一样。

解决方法很简单:

当您定义目标状态时,没有选项卡的状态:

.state('tab.target', {
    url: '/target',
    views: {
       'tab-target': {
        templateUrl: 'templates/tab-target.html',
        controller: 'TargetCtrl'
        }
    }
})

只需像另一种有标签的状态一样设置“views”的值,例如:

.state('tab.target', {
    url: '/target',
    views: {
       'tab-dash': {
        templateUrl: 'templates/tab-target.html',
        controller: 'TargetCtrl'
        }
    }
})

然后,当用户浏览到目标选项卡时,在选项卡栏中,“破折号”选项卡将显示为实际选项卡。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多