ItSolutionStuff.com

AngularJS Convert String to Object Example

By Hardik Savani • November 5, 2023
JSON Angular

Are you looking for how to convert string to json object using angular.fromjson() in angular js app. we will convert string to object using fromjson(). we can easily convert string to javascript object in angular.js.

few days ago i have php array but when i use in angular js code then it show me as string even i pass as php array. you can see what i was getting string:

{\"name\":\"Hardik\",\"email\":\"itsolutionstuff@gmail.com\" }

But i need to convert this string to json object so i can use on my angular js code. so i search on google and i found fromjson() of angular function and i make it done. You can also see how to can use angular.fromjson() for converting string to object.

Example:

<!doctype html>

<html>

<head>

<title>AngularJS Convert String to Object Example - ItSolutionStuff.com</title>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

</head>

<body ng-app='myapp'>

<div ng-controller="myCtrl"> </div>

</body>

<script type="text/javascript">

var app = angular.module('myapp', []);

app.controller('myCtrl', ['$scope', '$http', function ($scope, $http) {

$scope.myTextObj = "{\"name\":\"Hardik\",\"email\":\"itsolutionstuff@gmail.com\" }";

$scope.myObj = angular.fromJson($scope.myTextObj);

console.log($scope.myObj);

}]);

</script>

</html>

Output:

Object

email: "itsolutionstuff@gmail.com"

name: "Hardik"

I hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP AngularJS MySQL Pagination Example

Read Now →

AngularJS Dynamically Change Image SRC on Click Example

Read Now →

AngularJS Filter Change Date Format in Controller Example

Read Now →

AngularJS Remove Duplicates Object from Array Example

Read Now →

AngularJS Check and Uncheck All Checkboxes Example

Read Now →

AngularJS Check Checkbox is Checked or Not Example

Read Now →

AngularJS Drag and Drop Table Rows Example with Demo

Read Now →

PHP AngularJS Populate Dynamic Dropdown Example

Read Now →

How to Copy to Clipboard without Flash in AngularJS?

Read Now →

AngularJS Convert Comma Separated String to Array Example

Read Now →

AngularJS - How to Create Read More/Less Toggle using Directive?

Read Now →

AngularJS Update Bind Ng-model Input Value from JQuery Example

Read Now →

How to Hide Div After Some Time in AngularJS?

Read Now →

AngularJS Simple Datepicker Directive Example Tutorial

Read Now →

AngularJS Sorting(using Orderby Filter) Table Data Example

Read Now →