Angular.js – using angular.toJSON() function
A simple Angular.js example of converting a javascript object to a JSON.
<!DOCTYPE html>
<html ng-app="testApp">
<head>
<style>
</style>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta name="description" content="Tizen Angular.js example"/>
<title>Tizen Angular.js example</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/angular.min.js"></script>
<script>
var app = angular.module('testApp', []);
app.controller('AppController', function($scope) {
var sampleObj = {};
sampleObj.weight = 60;
sampleObj.height = 180;
sampleObj.colors = {};
sampleObj.colors.r = 100;
sampleObj.colors.g = 178;
sampleObj.colors.b = 245;
$scope.obj = sampleObj;
$scope.JSONifiedObj;
$scope.changeObjectToJSON = function() {
$scope.JSONifiedObj = angular.toJson($scope.obj, true);
}
});
</script>
</head>
<body>
<br><br>
<div ng-controller="AppController">
var sampleObj = {};<br>
sampleObj.weight = 60;<br>
sampleObj.height = 180;<br>
sampleObj.colors = {};<br>
sampleObj.colors.r = 100;<br>
sampleObj.colors.g = 178;<br>
sampleObj.colors.b = 245;<br>
<br><br>
<button ng-mousedown="changeObjectToJSON()">
Click this button to convert the above object to a JSON
</button>
<br><br>
{{ JSONifiedObj }}
</div>
</body>
</html>