我正在開發一個使用Ionic框架進行佈局設計的angularJS應用程序。出於某種原因,我的表單未被提交。該函數應該提交用戶名和密碼並將其發佈到聯機PHP文件中。這裏是我的代碼:表單提交不能在angularJS中工作?
模板:
<ion-view title="Login">
<ion-content overflow-scroll="true" padding="true" class="has-header">
<?php include '../lib/init.php'; ?>
<form class="list validate" ng-submit="submit()">
<input type="hidden" name="action" value="login">
<ion-list>
<label class="item item-input">
<span class="input-label">Username</span>
<input type="text" ng-model="username" name="username" placeholder="">
</label>
<label class="item item-input">
<span class="input-label">Password</span>
<input type="password" ng-model="password" name="password" placeholder="">
</label>
</ion-list>
<div class="spacer" style="height: 40px;"></div>
<button type="submit" id="login-button4" class="button button-royal button-block">Log in</button>
<a ui-sref="register" id="login-button5" class="button button-royal button-block button-clear">Dont have an account?</a>
<a ui-sref="forgotPassword" id="login-button6" class="button button-royal button-block button-clear">Forgot password?</a>
</form>
</ion-content>
</ion-view>
控制器:
.controller('loginCtrl', function($scope, $state, $stateParams, $ionicPopup, $timeout) {
//popup alert starts here
$scope.showAlert = function(status,message) {
var alertPopup = $ionicPopup.alert({
title: status,
template: message,
});
};
//popup alert ends here
$scope.submit = function() {
if ($scope.username && $scope.password) {
var url = 'my PHP file url';
var username= $scope.username;
var password= $scope.password;
$scope.list = [];
var dataString = 'username='+ username + '&password=' + password;
$scope.list.push(dataString);
$scope.text = '';
$http({
type: "POST",
url: url,
data: dataString,
success: function(data) {
try
{
alert(data);
}
catch(e)
{
alert(data);
}
}
});
}
};
})
路線:
.state('login', {
url: '/login.php',
templateUrl: 'templates/login.php',
controller: 'loginCtrl'
})
有誰知道怎麼回事?
這可能是因爲你還沒有定義你的'username'和'password'範圍變量,當用戶填充字段時,他們將不會被觀看和更新。嘗試添加'$ scope.username =''; $ scope.password ='';'在你的'$ scope.submit = fun ...'之前。 – Gavin