Convert object to json in angular

I am trying to convert an object as json in my method updateDetails

But i am getting undefined

in console.log

after conversion as json

.
What's wrong here? my cod ...
<w> HTML:

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

      

This is my json fromat:

enter image description here

I want to store my data with these names

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

      

But I'm getting Id

, ProjectName

, Client

, OnsiteCoordinator

undefined The.

+3


source to share


3 answers


You are passing data

as a parameter, so you shouldn't use a prefix $scope

. Instead, just use data.



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

      

+2


source


You need to print not , also it should not be $ scope.data when converting, it should be $scope.json

$scope.data

data



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

      

+2


source


To display decorations, pass the second parameter as true for angular.toJson(obj, true)

. The JSON output will contain newlines and spaces.

0


source







All Articles