简体   繁体   English

在Angular中将JSON格式的字符串数组转换为真实的JSON数组对象

[英]convert JSON format string array into real JSON array object in Angular

I'm trying to send to a web a JSON witch contains several 'objects' (there are no real objects yet). 我试图将包含多个“对象”(尚无真实对象)的JSON巫婆发送到网络。

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. 为了进行测试,我将发送一个字符串JSON并将其转换为真实的JSON对象。 It's my first angular project, I'm not sure if i did it ok? 这是我的第一个有角度的项目,我不确定我做得还好吗? How I should do it? 我应该怎么做?

Regards 问候

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM