controllerAs方法在其他人没有被调用时

时间:2016-11-15 14:47:18

标签: angularjs ionic-framework

我'用我的Ionic app试用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'
  }
}
  })

修改 事实证明问题出在Sublime Text:由于某种原因,有一个奇怪的缓存问题 - 我正在编辑文件,但是没有识别出这些变化。我很高兴我问过,因为现在我正在使用&#39; controlsAs:&#39;在$ stateProvider而不是&#39; controller:someController作为&#39;。谢谢大家的帮助!

1 个答案:

答案 0 :(得分:1)

在您的app.js中更改为:

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

我会解释一下。在您的控制器中,您将变量设置为&#39; this&#39;。你使用的名字可以是wowItsAVariable = this;但是当你连接你的控制器时,你可以使用一个完全不同的名称,例如controllerAs:&#39; wowSomeOtherName&#39;这就是你在HTML中引用它的方式。这个名字并不重要,我不会使用诸如“控制器”之类的名字。因为这并没有告诉任何人你试图引用的控制器。希望有所帮助。

相关问题