2016-11-15 33 views
1

我試着用我的Ionic應用程序firebase 3。 (我只是讓一個混亂 - 所以我想我不妨做一切從頭開始,如使用ControllerAs符號)控制器當其他人不在調用方法

在控制器中的一切工作正常。只是logUserOut方法不起作用 - 完全可以!我很茫然。

控制器:

.controller('DashCtrl', function($scope, $ionicModal, DataService, AuthService) { 
    var ctrl = this; 
    ctrl.icons = []; 

    //// SERVICE GETS 
    ctrl.allIcons = DataService.icons(); 
    ctrl.pages = DataService.pages(); 
    //// 

    //// FILTER INFORMATION 
    ctrl.loadIcons = function() { 
    console.log(ctrl.pages) 
    for (icon in ctrl.allIcons) { 
     var i = ctrl.allIcons[icon]; 
     // console.log(i) 
     if(i.active){ 
     ctrl.icons.push(i); 
     } 
    } 
    }; 
    //// 


    //// ACTIONS 
    ctrl.logUserOut = function(){ 
    console.log('this is not being called') 
    // AuthService.logout(); 
    }; 
    //// 
}); 

HTML:

<ion-modal-view> 

<ion-pane > 
<ion-content scroll="false" > 
<section class = "modal-container"> 
    <div class="item modal-header"> 
     <button class="button button-left button-icon light ion-android-close" ng-click="ctrl.closeProfileModal()"></button> 
     <div class="light text-center"> 
      <button class="button button-clear" ng-click="controller.closeProfileModal()"> 
      <img class="avatar-modal" src="img/mike.png"> 
      </button> 
     </div> 
    </div> 
    <div class = "row modal-profile-row"> 
     <div class = "col"> 
      <button class="button button-clear"> 
      <span class="title light">Personal Info</span> 
      </button> 
     </div> 
    </div> 

    <div class = "row modal-profile-row"> 
     <div class = "col"> 
      <button class="button button-clear" ng-click="ctrl.logUserOut()"> 
      <span class="title light">Sign Out</span> 
      </button> 
     </div> 
    </div> 
</section> 
</ion-content> 
</ion-pane> 
</ion-modal-view> 

app.js:

.state('tab.dash', { 
url: '/dash', 
views: { 
    'tab-dash': { 
    templateUrl: 'templates/tab-dash.html', 
    controller: 'DashCtrl', 
    controllerAs: 'ctrl' 
    } 
} 
    }) 

編輯: 原來的問題是與崇高的文字:某種原因,有沒有eird緩存問題 - 我正在編輯文件,但更改無法識別。我很高興我問了,因爲現在我在$ stateProvider中使用'controllerAs:',而不是'controller:someController'。感謝大家的幫助!

+0

你的'closeProfileModal'方法在哪裏? – Weedoze

+0

ControllerAs是一個不錯的第一步。下一步是將DashCtrl作爲Component而不是Controller:https://docs.angularjs.org/guide/component。然後$ ctrl.logUserOut()將正常工作。 –

+0

@ Weedoze我把它丟掉了,因爲它正在工作。 – rcpilotp51

回答

1

在你app.js更改爲:

.state('tab.dash', { 
url: '/dash', 
views: { 
'tab-dash': { 
    templateUrl: 'templates/tab-dash.html', 
    controller: 'DashCtrl', 
    controllerAs: 'ctrl' // <-- here 
} 
} 
}) 

我會解釋一下。在你的控制器中,你設置一個變量爲'this'。您使用的名稱可以是任何類型,如wowItsAVariable = this;然而,當你連接你的控制器時,你可以使用一個完全不同的名字,比如controllerAs:'wowSomeOtherName',這就是你在html中引用它的方式。這個名字並不重要,我會遠離使用諸如'控制器'這樣的名字,因爲它不會告訴任何人你想要引用的控制器。希望有所幫助。

+0

這有相同的結果。 – rcpilotp51

+0

對不起,您的html更改爲:

+0

沒錯,謝謝。仍然是相同的結果,並且該方法無法調用。 – rcpilotp51

相關問題