gpt4 book ai didi

javascript - Ionic 中的 $state.go 与 ion-nav-view 不起作用

转载 作者:行者123 更新时间:2023-11-28 00:34:12 25 4
gpt4 key购买 nike

我已经启动了一个 Ionic Tabs 项目。现在我有一个按钮“initiateProcess”,我用 ng-click 调用它,并在 Controller 中执行一些操作,然后进入特定状态(tab.target >) 与 state.gostate.transitionTo

但是,当我不在 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 中的函数(已注入(inject)$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>

最佳答案

通过禁用 $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。它是您 View 的容器。如果您决定命名它,那么您必须与 $stateProvider 中的 View 名称匹配。基本上 ionic 很困惑你想在哪里显示 tabs.target,因为没有匹配的容器

关于javascript - Ionic 中的 $state.go 与 ion-nav-view 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28658020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com