2017-06-12 74 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输出将包含换行符和空格。