2016-02-23 71 views
0
.controller('LoginConnect', ['$scope', 'connecting', 
function(connecting, $scope){ 
    $scope.user = {}; 
    var inputLogin = $scope.user.login; 
    var inputPassword = $scope.user.password; 
    $scope.connect = function(){ 
     connecting(ConnectingFactory); 
    }; 
    } 
]) 
.factory('connecting', ['$http','$q', function ($http,$q,inputLogin, inputPassword, ConnectingFactory){ 
var ConnectingFactory = {}; 
console.log(ConnectingFactory); 
ConnectingFactory.login = function(){ 
     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: inputLogin, password: inputPassword} 
      }) 
     .success(function(result){ 
      deferred.resolve(result); 
      var promise = deferred.promise; 
      promise.then(function(result){ 
      console.log(result); 
      // jsonTab = angular.fromJson(result); 
      // $scope.result = result["data"]; 
     // $scope.user.token = result["data"]; 
      }); 
     }) 
     }; 


     return ConnectingFactory; 

}]); 
; 

而且這裏的HTML:郵政法的角度JS

<!-- User Connection --> 
<form name="userConnect" ng-submit="connect()" novalidate ng-controller="LoginConnect"> 
    <label> 
    Enter your name: 
    <input type="text" 
      name="myEmail" 
      ng-model="user.login" 
      /> 
    </label> 

    <label> 
    Enter your Password: 
    <input type="password" 
      name="password" 
      ng-model="user.password" 
      /> 
    </label> 

<input type="submit" value="Connection"> 
<p>resultat : {{result}}</p> 
<p ng-model="user.token"> 
    token : {{mytoken}} 
</p> 


<p ng-model="user.datab"> 
    datas : {{datab}} 
</p> 
<br><br><br> 


</form> 

您好,我米的角的js DEVELOPPEMENT新的,我沒有錯誤,但在發送給API沒有任何數據。我認爲他們在我的函數「connect()」和工廠之間沒有任何聯繫。你能幫我嗎?

+2

在第二行代替這個函數(連接,$ scope){TRY函數($ scope,connecting){按照你注入的順序。 – Haris

+0

$ scope.connect = function(){connecting.login());}; –

回答

2

請不要使用成功方法。兩種方法都已被棄用。

$ http legacy legacy methods成功與錯誤已被棄用 。改爲使用標準然後方法。如果 $ httpProvider.useLegacyPromiseExtensions設置爲false,那麼這些 方法將拋出$ http/legacy錯誤。

這裏是快捷方法

$http.post('/someUrl', data, config).then(successCallback, errorCallback); 

這裏是一個較長GET方法樣品

$http({ 
    method: 'GET', 
    url: '/someUrl' 
}).then(function successCallback(response) { 
    // this callback will be called asynchronously 
    // when the response is available 
    }, function errorCallback(response) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
    }); 

Official Documentation

關於工廠,正確地把它作爲ConnectingFactory.login()

此外,這裏的順序是不正確的,正如Harry指出的那樣。

['$scope', 'connecting', 
function(connecting, $scope){