AngularJS范围变量不更新

时间:2013-01-13 04:51:44

标签: php javascript 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;
        });
};

正如所料,&#34; f&#34;出现在每个字段中。但是,如果您更改这些字段然后提交表单,服务器将响应(在帖子数据上使用print_r()),表明后端ALWAYS收到的JSON将包含&#34; f&#34;作为每个字段的值,而不是更改的值。

实施例: 用户名&#34;测试&#34;等

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

因此,当我输入表单时,$ scope中的值不会因某些原因而更新。是什么给了什么?

2 个答案:

答案 0 :(得分:1)

不要在$ http调用中分配单独的$ scope属性,而是在范围内使用“大对象”:$ scope.person = {username:'f',...}。然后你就可以发布那个对象了。

事实证明,这里的具体问题是angular不会将值写入范围变量,除非它们验证,在这种情况下,电子邮件没有正确验证。

答案 1 :(得分:0)

使用'ng-app'和'ng-controller'明确定义范围?

我在你的html中找不到这两个指令。也许你没有把它作为问题的一部分。

我期望的样本结构:http://angularjs.org/#todo-html

相关问题