angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲的實例
更新時間:2018年10月08日 14:19:54 作者:泠泠在路上
今天小編就為大家分享一篇angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
1.html:把json對象轉(zhuǎn)換成json字符串
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="angular.min.js"></script>
</head>
<body>
<div ng-app="module" ng-controller="ctrl">
{{data}}
</div>
<script>
var m = angular.module('module', []);
m.controller('ctrl', ['$scope', function ($scope) {
$scope.data = {'name': '泠泠在路上'};
//把json對象轉(zhuǎn)換成json字符串
localStorage.setItem('data', angular.toJson($scope.data));
}]);
</script>
</body>
</html>
2.html:字符串轉(zhuǎn)換成json對象
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="angular.min.js"></script>
</head>
<body>
<div ng-app="module" ng-controller="ctrl">
{{data.name}}
</div>
<script>
var m = angular.module('module', []);
m.controller('ctrl', ['$scope', function ($scope) {
//alert出字符串
alert(localStorage.getItem('data'));
//字符串轉(zhuǎn)換成json對象
$scope.data =angular.fromJson(localStorage.getItem('data'));
}]);
</script>
</body>
</html>
3.html字符串轉(zhuǎn)換成json對象
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="angular.min.js"></script>
</head>
<body>
<div ng-app="module" ng-controller="ctrl">
{{data.username}}
</div>
<script>
var m = angular.module('module', []);
m.controller('ctrl', ['$scope', function ($scope) {
//字符串轉(zhuǎn)換成json對象
$scope.data = angular.fromJson('{"username":"泠泠在路上"}');
}]);
</script>
</body>
</html>
以上這篇angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
AngularJS實現(xiàn)分頁顯示數(shù)據(jù)庫信息
這篇文章主要為大家詳細介紹了AngularJS實現(xiàn)分頁顯示數(shù)據(jù)庫信息效果的相關資料,感興趣的小伙伴們可以參考一下2016-07-07
angularjs下ng-repeat點擊元素改變樣式的實現(xiàn)方法
今天小編就為大家分享一篇angularjs下ng-repeat點擊元素改變樣式的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
angularJs中ng-model-options設置數(shù)據(jù)同步的方法
今天小編就為大家分享一篇angularJs中ng-model-options設置數(shù)據(jù)同步的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

