奇怪的错误在这里。我有这样的形式:AngularJS范围变量不更新
<form name="newaccount" id="newaccount" ng-submit="doSubmit()">
<label>username</label>
<input name="username" type="text" required ng-model="username">
<span ui-toggle="username.length > 15">Too long!</span>
<label>name</label>
<input name="name" type="text" required ng-model="name">
<label>e-mail</label>
<input name="email" type="email" required ng-model="email" ui-validate>
<label>password</label>
<input name="password" type="password" required ng-model="password" ui-validate>
<div style="text-align: center;"><br>
<input type="submit"></div>
</form>
而这个控制器:
$scope.username = "f";
$scope.password = "f";
$scope.email = "f";
$scope.name = "f";
$scope.doSubmit = function(){
//Transition to progress view
$scope.showLoginForm = false;
$scope.showCreateForm = false;
$scope.showLoading = true;
$scope.resultSuccess = false;
$scope.showResult = false;
$scope.loadingMessage = "Creating account...";
$scope.resultReason = "Success!";
//Send POST
$http({
method: 'POST',
url: "php/createaccount.php",
data: {
"username": $scope.username,
"password": $scope.password,
"name": $scope.name,
"email": $scope.email
}}
).success(function (data, status, headers, config) {
$scope.showLoading = false;
$scope.showResult = true;
$scope.resultSuccess = data === "success";
if($scope.resultSuccess){
$scope.resultReason = "Account created successfully!";
}else{
$scope.resultReason = data;
}
}).error(function (data, status, headers, config) {
$scope.showLoading = false;
$scope.showResult = true;
$scope.resultSuccess = false;
$scope.resultReason = data;
});
};
正如预期的那样, “F” 出现在每个字段。但是,如果您更改这些字段并提交表单,则服务器响应(在发布数据上使用print_r()),表明后端ALWAYS收到的JSON将包含“f”作为每个字段的值,而不是更改后的值。
示例: 使用用户名“测试”等
{"username":"f","password":"f","name":"f","email":"f"}
因此,在$范围的值没有得到更新由于某种原因,当我在形式键入。是什么赋予了?
是否有可能在每次提交表单时将字段赋值为“f”的代码运行? – akonsu
也许,如果我没有提交任何内容,但删除了该部分! (从字面上看,没有键或值) –
你能够在jsfiddle上设置此代码吗?不必发送POST,只需输出到控制台。 – akonsu