2017-05-15 29 views
0

我試圖調試我的代碼:我有我的簡單服務「密碼重置」在這裏:AngularJs:xxx是不是一個函數,但工廠注入和函數中聲明

(function() { 
    'use strict'; 

    angular 
     .module('app.login') 
     .factory('ResetPasswordService', ResetPasswordService); 

    function ResetPasswordService($http, $q) { 

     function sendMessage() { 

      var there = this; 
      var deferred = $q.defer(); 
      var data = { 
       email: this.email 
      }; 

      $http({ 
       method: 'POST', 
       url: 'password/email', 
       data: data 
      }) 
       .then(_loginSuccess, _loginFailed); 

      function _loginSuccess(response) { 
       console.log(response); 
       deferred.resolve(there); 
      } 

      function _loginFailed(response) { 
       deferred.reject('Une erreur interne est survenue' + response.error); 
      } 

      return deferred.promise; 
     } 

     return sendMessage; 

    } 
})(); 

而且我試圖用它在我控制器,當用戶點擊「是」(事件的onTap - 確認密碼重置)

(function() { 
    'use strict'; 

    angular 
     .module('app.login') 
     .controller('LoginCtrl', LoginCtrl); 

    function LoginCtrl(UserModel, $ionicPopup, $scope, ResetPasswordService) { 

     var vm = this; 
     vm.user = UserModel.buildUser(); 
     vm.login = login; 
     vm.forgotPassword = forgotPassword; 

     /** 
     * function called when user clicks on 'mot de passe oublié' button 
     * @function 
     * @name forgotPassword 
     * @memberOf App.Login.LoginCtrl 
     */ 
     function forgotPassword() { 

      vm.data = { 
       email : vm.user.email 
      }; 

      var myPopup = $ionicPopup.show({ 
       template: '<input type="email" ng-model="vm.data.email" placeholder="Entrez votre adresse email">', 
       title: 'Mot de passe oublié', 
       subTitle: 'Souhaitez vous réinitialiser' + ' <br> ' + 'votre mot de passe ?', 
       scope: $scope, 
       buttons: [ 
        {text: 'Non'}, 
        { 
         text: '<b>Oui</b>', 
         type: 'button-positive', 
         onTap: function (e) { 
          if (!vm.data.email) { 
           //don't allow the user to close unless he enters his email 
           e.preventDefault(); 
           console.log('Le champs est vide ! '); 
          } else { 
           /!\ HERE /!\ ResetPasswordService.sendMessage() /!\ HERE /!\ 
            .then(_success, _fail); 
           return vm.data.email; 
          } 
         } 
        } 
       ] 
      }); 

      myPopup.then(function (res) { 
       if (res) { 
        console.log('Tapped!', res); 
       } 
       else { 
        console.log('annulation'); 
       } 
      }); 
     } 
     } 

})(); 

但後來我有一個消息,告訴我,

ResetPasswordService.sendMessage不是功能。

,我已經注射了配套廠。我仍然是JS的新手,這不是我第一次看到這個消息,但我現在無法弄清楚......我又在做什麼錯了?

回答

3

從你的工廠,你應該返回

return { 
    sendMessage: sendMessage 
} 

目前,ResetPasswordService本身就是ResetPasswordService.sendMessage

+1

OOOOH所以這是它...你救我多一些時間ahaha。謝謝,我會記住我的錯誤!虐待標記爲接受一旦我允許:) :) – DevMoutarde

+0

@DevMoutarde很高興它有點幫助!如果你能夠upvote並且一旦允許,就會很棒。謝謝!當然是的,沒有看到更新的評論:) – tanmay

相關問題