我一直試圖使用離子angularjs中的$ http請求連接到後端服務器。我想要使用API註冊一個用戶。有人能告訴我我的代碼有什麼問題嗎?這是我的controller.js如何使用angularjs中的API連接到後端服務器
angular.module('app.Auth', [])
.factory('Auth', ['$q', '$http', '$localStorage', 'URL', function($q, $http, $localStorage, URL) {
return {
register: function(data, success, error) {
$http.post(URL.api + 'register', data).success(success).error(error)
}
};
}])
.controller('SignupCtrl', function($scope, $rootScope, $timeout, $stateParams, ionicMaterialInk,Auth,$localStorage,$state,Auth,URL) {
$scope.signupError = function(msg){
if(msg.status == false){
$scope.errors = msg.errors;
}else{
$scope.errors = ['Error accessing API.'];
}
};
$scope.signupSuccess = function(response){
if(response.status==false){
$scope.errors = response.errors;
}else{
alert("success!");
}
};
$scope.user = {
firstname:'',
middlename:'',
lastname:'',
password: '',
email: ''
};
$scope.signupEmail = function(){
Auth.register($scope.user,$scope.signupSuccess,$scope.signupError);
};
});
,這是我signup.html
<ion-view view-title="Sign Up" name="signup">
<ion-content>
<div class="list">
<div ng-if="errors.length > 0" class="error_bg" ng-repeat="error in errors">
<p class="error" ng-bind="error"></p>
</div>
<label class="item item-input item-floating-label">
<span class="input-label">First Name</span>
<input type="text" placeholder="First Name" style="color:purple; font-size:18px" ng-model="user.firstname">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Middle Name</span>
<input type="text" placeholder="Middle Name" style="color:purple; font-size:18px" ng-model="user.middlename">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Last Name</span>
<input type="text" placeholder="Last Name" style="color:purple; font-size:18px" ng-model="user.lastname">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Email Address</span>
<input type="text" placeholder="Email Address" style="color:purple; font-size:18px" ng-model="user.email">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Password</span>
<input type="password" placeholder="Password" style="color:purple; font-size:18px" ng-model="user.password">
</label>
</div>
<button class="button button-block button-royal" style="background-color:blue" ng-click="signupEmail()">Continue</button>
您將不得不提供更多信息。你遇到什麼問題?如果你只是不知道如何使用$ http,那麼有很多例子。如果您遇到錯誤,請發佈錯誤和導致錯誤的代碼。 –
好的我已經添加了詳細信息:) –
目前還不清楚你的問題是什麼。你有錯誤嗎? –