6

I am trying to convert object as json In my updateDetails method But am getting undefined in console.log after converting as json.
Whats wrong here?my cod is..

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 save my data with these names

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

But am getting Id,ProjectName,Client,OnsiteCoordinator is undefined.

3
  • please refer below link stackoverflow.com/questions/11819301/… Commented Jun 12, 2017 at 5:07
  • Yes I did the same thing here $scope.json = angular.toJson($scope.data); .why am getting undefined? Commented Jun 12, 2017 at 5:09
  • 1
    give preference to the answers posted on time Commented Jun 12, 2017 at 5:28

3 Answers 3

6

You are passing the data as a parameter, hence you should not use $scope prefix. 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();
                    }
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please try it using ng-show / ng-hide instead of ng-if.
But that is not my problem..I am struggled at $scope.json = { "project_id": Id, "name": ProjectName, "client": Client, "onsite_coordinator": OnsiteCoordinator }; here only
Sorry, I mean, while you using ng-if, the other ng- components will be executed only if the ng-if condition is true, else it will be skipped at initial compiling.
2

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

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

1 Comment

Sure Sajeetharan
0

For beautification output, pass second parameter as true for angular.toJson(obj, true). The JSON output will contain newlines and whitespace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.