javascript 单击离子导航栏中的按钮移动到新页面的所有 3 种方式都不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25350456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:24:49  来源:igfitidea点击:

All 3 ways of moving to new page on click of button in the ionic navbar dont work

javascriptangularjsionic-framework

提问by Thinkerer

Ideally, all 3 methods ought to work. This codepen below shows all 3 methods well.

理想情况下,所有 3 种方法都应该有效。下面的代码笔很好地展示了所有 3 种方法。

Correct and working CodePen Demo app

正确且有效的 CodePen 演示应用程序

Currently, neither of the 3 methods work; the navbar just dissappears upon clicking the button (shows empty nav bar) while the core page remains the same main page.

目前,这三种方法都不起作用;单击按钮后,导航栏就会消失(显示空的导航栏),而核心页面保持相同的主页面。

Im not sure if its an code problem, ionic issue or just simply I should not transit to a new page from a navbar. The last one is too illogical to accept though.

我不确定是代码问题、离子问题还是只是我不应该从导航栏转换到新页面。不过最后一个太不合逻辑了,无法接受。

Would any kind souls know where the issue lie and help me please?

有没有好心人知道问题出在哪里并帮助我?

My core content code in index.html

我在 index.html 中的核心内容代码

<body animation="slide-left-right-ios7">

        <ion-nav-bar class="bar-light nav-title-slide-ios7"></ion-nav-bar>

        <ion-nav-view></ion-nav-view>


The button html I have (Note all 3 versions were tested seperately)


我拥有的按钮 html(注意所有 3 个版本都经过单独测试)

    <ion-view ng-controller="NavCtrl"> 

     <ion-nav-buttons side="left">
      <button class="button button-icon ion-compose" ng-click="create('tab.newpost')"></button>
      <button class="button button-icon ion-compose" ui-sref="tab.newpost"></button>
      <button class="button button-icon ion-compose" href="/tab/newpost"></button>
     </ion-nav-buttons>

     <ion-content class>
        <!-- Rest of the content body here --> 
     </ion-content> 
    </ion-view>


Code in nav.js mainly for the state.create method


nav.js 中的代码主要针对 state.create 方法

app.controller('NavCtrl', function ($scope, $location, $state, Post, Auth) {
    $scope.post = {url: 'http://', title: ''};

    $scope.create = function(stateName) {
      /* $location.path('/tab/newpost'); */ 
      $state.go(stateName);  /* tried swapping stateName with 'tab.newpost' and func() */
    };
  });


Code for app.js (Route file)


app.js 的代码(路由文件)

    var app = angular.module('MyApp', ['ionic','firebase']);

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

        .state('tab', {
          url: '/tab',
          abstract: true,
          templateUrl: 'templates/tabs.html'
        })

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

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

        /* + other states .... */

$urlRouterProvider.otherwise('/auth/login');
});

回答by Seminda

First method you used according to the your code look like this

根据您的代码使用的第一种方法如下所示

ng-click="create('tab/newpost')"

It should be

它应该是

ng-click="create('tab.newpost')"

回答by ahmed wahba

i think you need to modify states name so you can navigate between them

我认为您需要修改状态名称,以便您可以在它们之间导航

var app = angular.module('MyApp', ['ionic','firebase']);

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

        .state('tab', {
          url: '/tab',
          abstract: true,
          templateUrl: 'templates/tabs.html'
        })

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

        .state('tab.newpost', {
          url: '/newpost',
          views: {
            'tab-posts':{            /* the same name of the above state */
              templateUrl: 'templates/tab-newpost.html',
              controller: 'NewCtrl'
            }
          }
        });

        /* + other states .... */

$urlRouterProvider.otherwise('/auth/login');
});