AngularJS强制后退按钮路线

时间:2016-09-17 16:14:58

标签: javascript angularjs ionic-framework

我的后退按钮将我发送到我的应用程序的第一页/状态(cadastreSe)。如何强制它返回上一页/状态('menu.temporada2016'),我的按钮id =“temporada2016-button3”处于活动状态。 angularjs有可能吗?

我想用$ scope启用它。$ on('$ ionicView.beforeEnter'...

状态

angular.module('app.routes', [])

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

$stateProvider


.state('menu.temporada2016', {
cache: false,
url: '/page1',
views: {
  'side-menu21': {
    templateUrl: 'templates/temporada2016.html',
    controller: 'temporada2016Ctrl',
    params: { dataToTemp: false }        
  }
}
})

.state('menu.fotos', {
url: '/page2',
views: {
  'side-menu21': {
    templateUrl: 'templates/fotos.html',
    controller: 'fotosCtrl'
  }
}
})

.state('menu.configuraEs', {
url: '/page3',
views: {
  'side-menu21': {
    templateUrl: 'templates/configuraEs.html',
    controller: 'configuraEsCtrl'
  }
}
})

.state('menu', {
url: '/side-menu21',
templateUrl: 'templates/menu.html',
abstract:true
})

.state('detalhes', {
cache: false,
url: '/page4',
params: {
  dataToDetails: false
},
templateUrl: 'templates/detalhes.html',
controller: 'detalhesCtrl'
})

.state('cadastreSe', {
url: '/page5',
templateUrl: 'templates/cadastreSe.html',
controller: 'cadastreSeCtrl'
})

.state('home', {
url: '/page6',
templateUrl: 'templates/home.html',
controller: 'homeCtrl'
})

.state('suporte', {
url: '/page7',
templateUrl: 'templates/suporte.html',
controller: 'suporteCtrl'
})

.state('fotos2', {
url: '/page8',
templateUrl: 'templates/fotos2.html',
controller: 'fotos2Ctrl',
params: {
  dataToFotos: false
}
})

$urlRouterProvider.otherwise('/page5')



});

控制器

.controller('fotos2Ctrl', ['$scope', '$state', '$ionicModal', '$ionicSlideBoxDelegate', '$ionicScrollDelegate', function ($scope, $state, $ionicModal, $ionicSlideBoxDelegate, $ionicScrollDelegate) {

$scope.$on('$ionicView.beforeEnter', function(event, viewData){
    viewData.enableBack = true;
});

$scope.$on('$routeChangeSuccess', function(event, current, previous){


});

if(!$state.params.dataToFotos) {
    console.log($state.params.dataToFotos);

}else{

    console.log($state.params.dataToFotos);
    $scope.images = $state.params.dataToFotos.album;         
}

$scope.zoomMin = 1;

// image gallery
$scope.showImages = function(index) {
    $scope.activeSlide = index;
    $scope.showModal('templates/popover.html');
}

$scope.showModal = function(templateUrl) {
    $ionicModal.fromTemplateUrl(templateUrl, {
        scope: $scope,
        animation: 'slide-in-up'
    }).then(function(modal) {
        $scope.modal = modal;
        $scope.modal.show();
    });
}

// Close the modal
$scope.closeModal = function() {
    $scope.modal.hide();
    $scope.modal.remove()
};

$scope.updateSlideStatus = function(slide) {

    var zoomFactor = $ionicScrollDelegate.$getByHandle('scrollHandle' + slide).getScrollPosition().zoom;

    if (zoomFactor == $scope.zoomMin) {
        $ionicSlideBoxDelegate.enableSlide(true);
    } else {
        $ionicSlideBoxDelegate.enableSlide(false);
    }
};

}])

fotos2.html

<ion-view title="Fotos ..." id="page8" style="background-color:#FFFFFF;">
 <ion-content padding="true" class="has-header">
    <!--<div class="row responsive-md">
        <div class="col col-25" ng-repeat="image in images">
            <img ng-src="http://localhost/dashboard/{{image.FILE}}" width="100%" />
        </div>
    </div>-->
    <div class="row" ng-repeat="image in images" ng-if="$index % 4 === 0">
        <div class="col col-25" ng-if="$index < images.length">
            <img class="grid-thumb" ng-src="http://localhost/dashboard/{{images[$index].FILE}}" width="100%" ng-click="showImages($index)" />
        </div>
        <div class="col col-25" ng-if="$index + 1 < images.length">
            <img class="grid-thumb" ng-src="http://localhost/dashboard/{{images[$index + 1].FILE}}" width="100%" ng-click="showImages($index+1)" />
        </div>
        <div class="col col-25" ng-if="$index + 2 < images.length">
            <img class="grid-thumb" ng-src="http://localhost/dashboard/{{images[$index + 2].FILE}}" width="100%" ng-click="showImages($index+2)" />
        </div>
        <div class="col col-25" ng-if="$index + 3 < images.length">
            <img class="grid-thumb" ng-src="http://localhost/dashboard/{{images[$index + 3].FILE}}" width="100%" ng-click="showImages($index+3)" />
        </div>
    </div>
 </ion-content>
</ion-view>

temporada2016.html

<ion-view title="Temporada 2016" id="page1" style="background-color:#FFFFFF;">
 <ion-content padding="true" class="has-header">
    <div id="temporada2016-button-bar1" class="button-bar">
        <button id="temporada2016-button2" style="color:#008BBB;" class="button button-light  button-block button-outline" ng-class="{active_news_btn: active_news_btn}" ng-click="activate_news()">Notícias</button>
        <button id="temporada2016-button3" style="color:#008BBB;" class="button button-light  button-block button-outline" ng-class="{active_albums_btn: active_albums_btn}" ng-click="activate_albums()">Fotos</button>
    </div>

    <form id="temporada2016-form8" class="list" ng-show="search_news">
        <label class="item item-input" name="search_news">
            <i class="icon ion-search placeholder-icon"></i>
            <input type="search" placeholder="Pesquisar Notícia" ng-model="searchNews">
        </label>
    </form>

    <div class="item item-body list-container" id="temporada2016-list-item-container4" ng-model="item_id" ng-repeat="x in items | filter:searchNews" item="x" href="#/x/{{x.ID}}" ng-click="open_item(x)" ng-show="news_list">

        <div id="temporada2016-markdown7" style="margin-top:0px;color:#666666;">
            <h2 style="color:#008BBB;">{{ x.TITLE }}</h2>
        </div>

    </div>


    <!--<ion-list class="widget uib_w_1 d-margins" data-uib="ionic/list" data-ver="0">
        <ion-item class="item widget uib_w_2" data-uib="ionic/list_item" data-ver="0" ng-repeat="x in items">{{ x.TITLE }}</ion-item>
    </ion-list>-->

    <ion-list id="fotos-list4" ng-show="albums_list">
        <ion-item class="item-icon-left item-icon-right calm" id="fotos-list-item4" ng-model="album_name" ng-repeat="item in albums" item="item" href="#/item/{{item.FOLDER}}" ng-click="open_album(item)">
            <i class="icon ion-images"></i>
                {{item.FOLDER}}
            <i class="icon ion-ios-arrow-forward"></i>
        </ion-item>
    </ion-list>
 </ion-content>
</ion-view>

的index.html

<body ng-app="app" animation="slide-left-right-ios7">
 <div>
  <div>
     <ion-nav-bar class="bar-calm">
        <ion-nav-back-button class="button-icon icon ion-ios-arrow-back"></ion-nav-back-button>
     </ion-nav-bar>
     <ion-nav-view></ion-nav-view>
    </div>
  </div>
</body>

1 个答案:

答案 0 :(得分:0)

您可以通过向特定视图的模板添加新页面来覆盖特定页面的标题。它应该在标签之外和之上。只需让它镜像你的正常标题。然后实现一个看起来完全相同的后退按钮,但是它会触发一个ng-click事件,然后您可以使用$ state.go将用户路由到您希望他返回的特定视图。 )或使用$ ionicHistory依赖性导航回导航堆栈。