将对象转换为Angular中的json [英] Convert object to json in angular
本文介绍了将对象转换为Angular中的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在我的updateDetails
方法中将对象转换为json,但是在转换为json
后却在console.log
中获取了undefined
. br>
HTML:
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
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的json:
This is my json fromat:
我想用这些名称保存数据
I want to save my data with these names
if ($scope.json) {
$scope.json = { "project_id": Id, "name": ProjectName, "client": Client, "onsite_coordinator": OnsiteCoordinator };
}
但是获取Id
,ProjectName
,Client
,OnsiteCoordinator
的定义是不确定的.
But am getting Id
,ProjectName
,Client
,OnsiteCoordinator
is undefined.
推荐答案
您正在传递data
作为参数,因此不应使用$scope
前缀.而是只使用数据.
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();
}
这篇关于将对象转换为Angular中的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文