2016-11-15 67 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