2016-03-03 69 views
1

我正在開發Ionic應用程序,並且完全使用工作狀態的左菜單。Ionic stateProvider - 動態顯示/隱藏返回按鈕

這裏的菜單代碼:

<ion-side-menu side="left"> 
    <ion-header-bar class="bar-royal"> 
     <h1 class="title">Example</h1> 
    </ion-header-bar> 
    <ion-content> 
     <ion-list> 
      <ion-item menu-close ng-repeat="test in data" 
         ng-switch="test.ID" 
         ng-href="{{test.ID == 0 ? '#/app/test' : '#/app/details/' + test.iID}}" 
         class="item item-icon-right"> 
       <div ng-switch-when="0"><span>{{test.Name}}</span></div>  
       <div ng-switch-default><span>{{test.Name}}</span> 
       </div> 
      </ion-item> 
     </ion-list>  
    </ion-content> 
</ion-side-menu> 

非常基本的設置。

,並與國碼(coffescript):

angular.module('hgApp', ['ionic']) 
.config [ 
'$stateProvider' 
'$urlRouterProvider' 
($stateProvider, $urlRouterProvider) -> 
    $stateProvider.state('app', 
     url: '/app' 
     abstract: true 
     templateUrl: './sections/menu/menu.html' 
     controller: 'menuList' 
    ).state('app.details', 
     url: '/details/:testID' 
     views: 
     'menuContent': 
      templateUrl: './sections/details/testDetails.html' 
      controller: 'testDetails' 

現在,在這裏我的問題:

我可以從列表左側菜單中,或者從我的鏈接導航到app/details/{testID}在我的主頁上。

只有當我使用主頁上的鏈接到達頁面時,我需要能夠向app/details/{testID}的頂部欄添加後退按鈕,但是如果我從左側菜單列表中找到該頁面,則不能。

兩個菜單列表,並在主頁上用作HREF "#/app/details/{{test.ID}}"

我如何能做到這一點的聯繫?我真的沒有任何線索。

在此先感謝您的幫助。

回答

1

所有的可能性,嗨,我也有類似的情況,我不得不隱藏按鈕,如果形式爲可編輯的,所以我做了以下

<ion-nav-back-button ng-show="!$root.hideBack"></ion-nav-back-button> 

Controller.js

$scope.makeEditable = function() { 
    $scope.isEditable = true; 
    $rootScope.hideBack = true; 
}; 

所以如果字段是可編輯的後退按鈕將消失。我希望這有任何幫助。

另一個例子是:

使用$ionicHistory在控制器中調用$state.go('app.details')之前。我想你有不同的主頁控制器,你使用$stage.go去詳細頁面?所以代碼應該是這個樣子

app.controller('HomeCtrl', ["$scope", "$ionicHistory", function($scope, $ionicHistory) { 

    $scope.goToDetails = function() { 
     $ionicHistory.nextViewOptions({ 
     disableBack: true 
     }); 
     $state.go('app.details'); 
    } 

}]); 

的Html

<button ng-click="goToDetails()"></button> 
+0

能否請您解釋這一點更好?我正在談論使用disableBack的例子。現在從你的答案中可以清楚地看出。 – Nick

+0

我已經更新了我的答案,希望這次有點清楚。 –

0

的方式

我認爲你可以完成你想要使用$廣播

基本行爲有趣的問題,你有當您單擊的鏈接,會告訴testDetails控制器「上調用的函數哎,我們來自一個鏈接,做你的東西」

的testDetails控制器將被期待這個調用,並顯示相應按鈕

原始代碼:

app.controller('AnyCtrl', 
function AnyCtrl ($scope) { 
    // Call this code on link click 
    $scope.$broadcast('fromlink', 'Some data'); 
}); 

app.controller('testDetails', 
    function testDetails ($scope) { 

    $scope.$on('fromlink', function (event, data) { 
    // Add button. Profit 
    }); 

}); 

你可以找到一個鏈接到一個小文章涵蓋這裏

https://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/