-2

I wonder, why not working when input has default value assign to controller and when you post this input undefined in the server side. However, when you type directly to input its working and got the value. What is the difference why assigning a default value undefined when you post to the server side.

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

Controller

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

  }
Rye
  • 179
  • 3
  • 17
  • Can you provide a snippet of your controller and how you send your data to the server? – Giovani Vercauteren Apr 25 '18 at 06:50
  • Please rephrase your question and include your code. Try to limit the question to the specific problem that you're facing. The current post is difficult to understand. – 31piy Apr 25 '18 at 06:53
  • because you surely arent assigning value to `Data.defval` inside controller. Share ur controller code – Shashank Vivek Apr 25 '18 at 06:56
  • sorry my fault. the code its working fine when sending data to the server when you type directly the value in the input. However, when you assign a default value its undefined in the server. – Rye Apr 25 '18 at 07:19
  • assigning a value to Data.defval is working fine @ShashankVivek. – Rye Apr 25 '18 at 07:33
  • You mean if you do not use `$http.post("Add.php", formdata)` and just call `$http.post("Add.php", $scope.Data)`, it will be undefined in server side? – Saeed Apr 25 '18 at 07:44
  • Hi @Saeed.At the http post is working fine. I think its weird because the input is working fine if you type the value to the input. The only problem, if you assign a value to the input (not typing), the input is undefined in the server. – Rye Apr 25 '18 at 07:54

2 Answers2

2

Don't use ng-value, refer this

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

and in controller

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

}
Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
  • I tried your initialize code $scope.Data.defval=1; but its undefined property defval. However, i came up on this. I think its the same as yours but this is working $scope.Data = {defval: '1'}; THanks @Shashank Vivek – Rye Apr 25 '18 at 08:48
  • @Rye: Both declaration are the same. The reason was I assigned `number` to the variable where as the `input` field wants it as `text` . So updated the question with `$scope.Data.defval='1';` It'll work. ;). Thanks for your feedback mate – Shashank Vivek Apr 25 '18 at 09:45
0

You can remove ng-value of element and just change this line of code from

var formdata = $scope.Data;

To

var formdata = ($scope.Data && $scope.Data.defval)? $scope.Data : {defval: 1};
Saeed
  • 5,413
  • 3
  • 26
  • 40
  • Thanks @ Saeed.At. i tried your code but undefined defval on console log. This initialize work for me $scope.Data = {defval: '1'}; – Rye Apr 25 '18 at 08:50