0

I'm trying to send to a web a JSON witch contains several 'objects' (there are no real objects yet).

This is my array:

$scope.atributs = [];

When I click on the button this is executed:

$scope.addRow = function(){
                        var newRow = {
                            nomAtribut: "",
                            tipus: "",
                            mida: "",
                            prioritat: "",
                            obligatori: "",
                            observacions: ""
                        }
                        $scope.atributs.push(newRow);
                    }

Then I have a second one button, when I click it this is happening:

$scope.sendRow = function(){
                        var d = {
                                    'nomAtribut': 'Saab',
                                    'tipus': 'String',
                                    'mida': '15',
                                    'prioritat': '1',
                                    'obligatori': 'No'
                                };
                        $http.post("http://10.0.203.73/WS/ws.php/tipusactius/alta", angular.toJson(d)).success(function(data){
                            $scope.status = data;
                        })
                    }

For testing I'm sending one single string JSON transforming it to real JSON object. It's my first angular project, I'm not sure if i did it ok? How I should do it?

Regards

jcobo1
  • 1,065
  • 1
  • 18
  • 34

1 Answers1

0

I put simple example.

var app = angular.module("app",[]);

app.controller("MyCtrl" , function($scope){
  
   $scope.data ={
       atributs :[{
         nomAtribut: "",
         tipus: "",
         mida: "",
         prioritat: "",
         obligatori: "",
         observacions: ""}]
   };
  
  $scope.addRow = function(index){
    var row = {
         nomAtribut: "",
         tipus: "",
         mida: "",
         prioritat: "",
         obligatori: "",
         observacions: ""};
       if($scope.data.atributs.length <= index+1){
            $scope.data.atributs.splice(index+1,0,row);
        }
    };
  
  $scope.sendRow = function(){
     $http.post("http://10.0.203.73/WS/ws.php/tipusactius/alta",$scope.data).
         success(function(data){
                            $scope.status = data;
                        })
        }
    
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
   <table>
     <tr ng-repeat="name in data.atributs track by $index">
        <td> <input type="text" ng-model="data.atributs[$index].nomAtribut"></td>
         <td> <input type="text" ng-model="data.atributs[$index].tipus"></td>
        <td> <input type="text" ng-model="data.atributs[$index].mida"></td>
        <br>
        <td> <input type="text" ng-model="data.atributs[$index].prioritat"></td>
         <td> <input type="text" ng-model="data.atributs[$index].obligatori"></td>
        <td> <input type="text" ng-model="data.atributs[$index].observacions"></td>
        <td> <input type="button" ng-click="addRow($index)" value="Add" ng-show="$last"></td>
     </tr>
   </table> 
    <span>{{data|json}}</span>
</div>
Hadi J
  • 16,989
  • 4
  • 36
  • 62