Ng-model不更新控制器中的值

Ng-model not updating the value in the controller

本文关键字:控制器 更新 Ng-model      更新时间:2023-09-26

我有一个这样的表单:

 <form name="UserForm" class="form-horizontal" ng-submit="setUser()" >
   <input id="firstname" name="firstname" type="text" placeholder="add a firstname.." class="form-control input-md" ng-model="user.firstname"  required>
       <button id="Submit" name="Submit" class="btn btn-primary" ng-click="showMessage = 'false'" ng-disabled="UserForm.$invalid">Save</button>
  </form>

和在我的控制器中:

customModule2.controller('UserController',function ($state, $stateParams, $location, $log,$http, $scope, userFactory , appSettings) {
 $scope.user = {};
 $scope.setUser = function () {
    if($scope.UserForm.$valid) {
        userFactory.addUser($scope.user)
            .success(function (data) {
                console.log("controller set : " + this.user.firstname);
                $scope.addMessage = data;
                $log.log('');
            })
            .error(function (data, status) {
                console.log("user firstname : " + $scope.user.firstname);
             });
        $scope.UserForm.$setPristine();
    }
 };
)};

现在,当我输入firstname时,我可以看到它被添加,但当我在控制台检查时,控制器中的值是空的。

任何帮助都将非常感激。谢谢

EDIT:

我有这个控制器,它的代码几乎是一样的,它工作得很好:

customModule2.controller('ProjectController',function ($state, $stateParams, $location, $log,$http, $scope, projectFactory , appSettings) {
    $scope.project = {};
    $scope.setProject = function () {
    if($scope.ProjectForm.$valid) {
        projectFactory.addProject($scope.project)
            .success(function (data) {
                $scope.addMessage = data;
                console.log('here' + $scope.project.name);
            })
            .error(function (data, status) {
                $scope.addMessage = "Erreur lors de l'ajout : " + data + ' ' + status;
                console.log('here');
                $log.log(data.error + '' + status);
            });
        $scope.showMessage = true;
        $scope.ProjectForm.$setPristine();
    }
    };

可能是$scope的问题吗?

可能success(...)error(...)函数有不同的作用域(不同的$scope变量)。要解决这个问题,可以缓存该值。

customModule2.controller('UserController',function ($state, $stateParams, $location, $log,$http, $scope, userFactory , appSettings) {
    $scope.user = {};
    $scope.setUser = function () {
       var firsName = $scope.user.firstname;
       if($scope.UserForm.$valid) {
           userFactory.addUser($scope.user)
               .success(function (data) {
                   console.log("controller set : " + this.user.cin);
                   $scope.addMessage = data;
                   $log.log('');
               })
               .error(function (data, status) {
                   console.log("user firstname : " firstName);
                });
           $scope.UserForm.$setPristine();
       }
    };
)};

您必须尝试在控制台中使用

打印您的用户名:
console.log("controller set : ", $scope.user.firstname);

连接console.log("controller set : " + $scope.user.firstname)试图将您的user.firstname对象转换为字符串。