簡體   English   中英

在Angular中將JSON格式的字符串數組轉換為真實的JSON數組對象

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

我試圖將包含多個“對象”(尚無真實對象)的JSON巫婆發送到網絡。

這是我的數組:

$scope.atributs = [];

當我單擊按鈕時,將執行以下命令:

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

然后有第二個按鈕,當我單擊它時,正在發生:

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

為了進行測試,我將發送一個字符串JSON並將其轉換為真實的JSON對象。 這是我的第一個有角度的項目,我不確定我做得還好嗎? 我應該怎么做?

問候

我舉個簡單的例子。

 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