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

12
  • may be help u stackoverflow.com/questions/32470928/… Commented Mar 2, 2016 at 12:37
  • What are you actually trying to achieve? Your variable in sendRow is still an object, not a string. Why do you want to try and create a string and then parse it to JSON? Commented Mar 2, 2016 at 12:42
  • I want to make a JSON. inside this JSON put all the 'rows' from the 'atributs' array Commented Mar 2, 2016 at 12:44
  • do you see my example? Commented Mar 2, 2016 at 12:45
  • I'm looking at it. I'm on the phone now, I'm sorry. But looks useful I will try it Commented Mar 2, 2016 at 12:46

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

1 Comment

do you want like this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.