Ng模型不更新控制器中的值

时间:2016-09-30 16:12:28

标签: javascript html angularjs

我有一个这样的表格:

 <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时,我可以看到它被添加但是当我在控制台中检查时,控制器中的值为null。

任何帮助都会受到赞赏。谢谢

编辑:

我有这个控制器,它的代码几乎相同,效果很好:

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问题吗?

2 个答案:

答案 0 :(得分:0)

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();
       }
    };
)};

答案 1 :(得分:0)

您必须尝试使用​​以下命令在控制台中打印您的用户名:

console.log("controller set : ", $scope.user.firstname);

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