使用角度js发布时,默认值输入未定义结果

时间:2018-04-25 06:48:55

标签: angularjs

我想知道,为什么在输入默认值分配给控制器时以及在服务器端发布未定义的输入时不工作。但是,当您直接输入以输入其工作并获得值时。当您发布到服务器端时,为什么分配默认值为undefined有什么不同。

<input type="text" id="defval" name="defval" ng-value="myvar" ng-model="Data.defval">

控制器

    app.controller('RoomContrroller', function($scope, $http){
$scope.myvar=1;
  $scope.SaveRecord = function (msg) {
          $scope.loaderForm=true;
          var formdata = $scope.Data;
          $http.post("Add.php", formdata)
            .then(function(response){

              console.log(response.data);
              if(response.data==0){
                  $scope.fetch();
                  $scope.$emit('swalSuccess', { message: msg });
              }else{
                  $scope.$emit('swalError', { message: msg });
              }
              $scope.loaderForm=false;
          });
      }

  }

2 个答案:

答案 0 :(得分:2)

不要使用ng-value,refer this

<input type="text" id="defval" name="defval" ng-model="Data.defval">

并在控制器中

app.controller('RoomContrroller', function($scope, $http){
$scope.Data.defval='1'; // initialize here <-------------
$scope.SaveRecord = function (msg) {
      $scope.loaderForm=true;
      var formdata = $scope.Data;
      $http.post("Add.php", formdata)
        .then(function(response){

          console.log(response.data);
          if(response.data==0){
              $scope.fetch();
              $scope.$emit('swalSuccess', { message: msg });
          }else{
              $scope.$emit('swalError', { message: msg });
          }
          $scope.loaderForm=false;
      });
  }

}

答案 1 :(得分:0)

您可以移除ng-value元素,只需从

更改此行代码即可
var formdata = $scope.Data;

var formdata = ($scope.Data && $scope.Data.defval)? $scope.Data : {defval: 1};