簡體   English   中英

AngularJS范圍變量未更新

[英]AngularJS Scope Variables not Updating

奇怪的錯誤在這里。 我有這個表格:

<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”作為每個字段的值,而不是更改的值。

示例:用戶名“ test”等

{"username":"f","password":"f","name":"f","email":"f"}

因此,當我鍵入表單時,$ scope中的值由於某種原因不會被更新。 是什么賦予了?

不用在$ http調用中分配單個$ scope屬性,而是在范圍上使用“大對象”:$ scope.person = {用戶名:'f',...}。 然后,您可以發布該對象。

事實證明,其中的一個具體問題是angular不會將值寫入作用域變量,除非它們經過驗證,在這種情況下,電子郵件無法正確驗證。

是否使用“ ng-app”和“ ng-controller”明確定義了范圍?

我在您的html中找不到這兩個指令。 也許您沒有將其作為問題的一部分。

我期望的示例結構: http : //angularjs.org/#todo-html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM