重复使用来自$ q promise

时间:2016-02-29 09:29:34

标签: angularjs angularjs-scope

//controller pour connection to API
.controller('LoginConnect', ['$scope', 'connecting',
function($scope,connecting,sendtoken){
    $scope.user = {};
    var users = $scope.user;
    $scope.connect = function (users) {
      var log = $scope.user.login;
      var pass = $scope.user.password;
      var mydata = {};
      connecting.login(log,pass).then(function(result){
        console.log(result);
        var montoken = result.data.token;
         sessionStorage.setItem('token',montoken);
         console.log(montoken);
      });
      var mytoken = sessionStorage.getItem('token');
      sendtoken.send(mytoken).then(function(userdata){
        $scope.datab = userdata;
      });
    };
  }
])

  //factory pour aller chercher le token
.factory('connecting', ['$http','$q', function ($http,$q){
      var ConnectingFactory = {};
      ConnectingFactory.login = function(log,pass){
       var deferred = $q.defer();
       $http({
           method: 'POST',
           url: "http://api.tiime-ae.fr/0.1/request/login.php",
           headers: {'Content-Type': 'application/x-www-form-urlencoded'},
           transformRequest: function(obj) {
               var str = [];
               for(var p in obj)
               str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
               return str.join("&");
           },
           data: {login: log, password: pass}
           })
       .success(function(result){
          deferred.resolve(result);
          // var promise = deferred.promise;
          // promise.then(function(result){
          // var  mydata = result["data"];
          // console.log(mydata);
          //   }
          //);
        });
        return deferred.promise;
       };
       return ConnectingFactory;

}])
    //END factory pour aller chercher le token

    //Factory pour envoyer le token
    .factory('sendtoken', ['$http','$q', function ($http,$q){
          var tokenreceipt = {};
          tokenreceipt.send = function(mytoken){
           var deferred = $q.defer();
           $http({
               method: 'POST',
               url: "http://api.tiime-ae.fr/0.1/request/settings-get.php",
               headers: {'Content-Type': 'application/x-www-form-urlencoded'},
               data: {token : mytoken}
               })
               .success(function(userdata){
                  deferred.resolve(userdata);
                  // var promise = deferred.promise;
                  // promise.then(function(result){
                  // var  mydata = result["data"];
                  // console.log(mydata);
                  //   }
                  //);
                });
                return deferred.promise;
               };
               return tokenreceipt;
    }]);
      //END Factory pour envoyer le token


;

嗨,我是angularJS的新手,我正在尝试将我的var“mytoken”(我的第一家工厂的结果)发送到我的第二家工厂,但它不起作用你可以帮助我吗?我不确定,但我应该链接承诺不是吗?

1 个答案:

答案 0 :(得分:1)

您忘记将依赖注入传递给控制器​​“sendtoken”

尝试使用app.controller('LoginConnect', ['$scope', 'connecting','sendtoken' function($scope,connecting,sendtoken){