2017-06-12 103 views
2

我試圖將對象轉換爲json在我的updateDetails方法中,但在獲得console.logundefined轉換爲json。 ?
請告訴我錯在這裏我的鱈魚是..

HTML:將角色轉換爲json角色

<body ng-app="myApp" ng-controller="myCtrl" ng-init="init()"> 

       <form id="show_details" ng-repeat="data in editProjDetails"> 
        <div> 
         <label><input type="text" class="form-control projectName required onlyAlphaNumeric" ng-model="data.ProjectName" ng-disabled="all"></label> 
        </div> 
        <div> 
         <label><input type="text" class="form-control client required onlyAlphabets" ng-model="data.Client" ng-disabled="all"></label> 
        </div> 
        <div id="projectCoOrdBlock"> 
         <label><input type="text" class="form-control projectCoOrd onlyAlphabets" ng-model="data.ProjectCoordinator" ng-disabled="true"></label> 
        </div> 
        <div> 
         <label><input type="text" class="form-control required onsiteCoOrd onlyAlphabets" ng-model="data.OnsiteCoordinator" ng-disabled="all"></label> 
        </div> 
        <div id="resourceBlock"> 
         <label><input type="text" class="form-control resource onlyNumeric" ng-model="data.ResourceAllocated" ng-disabled="true"></label> 
        </div> 
        <div> 
         <span class="pull-right btnMarginTop"> 
          <button class="btn btn-primary" id="projectDetailsEdit" ng-if="!editMode" ng-click="editDetails()">Edit</button> 
          <button class="btn btn-primary" id="projectDetailsUpdate" ng-if="editMode" ng-click="updateDetails(data)">Update</button> 
         </span> 
        </div> 
       </form> 
</body> 

SCRIPT

var app = angular 
        .module("myApp", []) 
        .controller("myCtrl", function ($scope, $http) { 
         $scope.editMode = false; 
         $scope.all = true; 
         $scope.init = function() { 
          $scope.getId(); 
         } 
         $scope.getId = function() { 
          var url = document.URL; 
          var id = /id=([^&]+)/.exec(url)[1]; 
          var result = id ? id : ' '; 
          $scope.getProjectDetails(result); 
         } 

         $scope.goEvent = function() { 
          $scope.editMode = !$scope.editMode; 
         } 
         $scope.updateDetails = function (data) { 
          debugger 
          $scope.editedArrayDetails = []; 
          $scope.editedArrayDetails.push(data); 
          $scope.json = angular.toJson($scope.data); 
          console.log($scope.data) 
          $scope.goEvent(); 
         } 
        }) 

這是我的JSON fromat:

enter image description here

我想用這些名字保存我的數據

if ($scope.json) { 
           $scope.json = { "project_id": Id, "name": ProjectName, "client": Client, "onsite_coordinator": OnsiteCoordinator }; 
          } 

,但我越來越IdProjectNameClientOnsiteCoordinator是不確定的。

+0

請參閱下面的鏈接https://stackoverflow.com/questions/11819301/how-to-use-angular-tojson-on-a-angular-controller-or-scope –

+0

是的,我做了同樣的事情在這裏'$ scope.json = angular.toJson($ scope.data);'。爲什麼要得到'undefined'? – user7397787

+1

給予偏好答案發布時間 – Sajeetharan

回答

2

您傳遞data作爲參數,因此您不應使用$scope前綴。而只是使用數據。

$scope.updateDetails = function (data) { 
         $scope.editedArrayDetails = []; 
         $scope.editedArrayDetails.push(data); 
         $scope.json = angular.toJson(data); 
         console.log($scope.json) 
         $scope.goEvent(); 
        } 
+0

你可以請檢查我更新的問題? – user7397787

+0

你可以請用ng-show/ng-hide代替ng-if。 –

+0

但是那不是我的問題。 。我掙扎在'$ scope.json = {「project_id」:id,「name」:ProjectName,「client」:Client,「onsite_coordinator」:OnsiteCoordinator};'Here only – user7397787

2

您需要打印$scope.json沒有$scope.data,也應該不是$ scope.data,當您轉換,應該是data

$scope.json = angular.toJson(data); 
console.log($scope.json) 
+0

Sure Sajeetharan – user7397787

+0

但我已經mared,下次我會;-) ;-) – user7397787

+1

你仍然可以標記和標記!只是一個信息 – Sajeetharan

0

對於美化輸出,在angular.toJson(obj, true)中傳遞第二個參數爲true。 JSON輸出將包含換行符和空格。