angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲(chǔ)的實(shí)例
1.html:把json對(duì)象轉(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對(duì)象轉(zhuǎn)換成json字符串
localStorage.setItem('data', angular.toJson($scope.data));
}]);
</script>
</body>
</html>
2.html:字符串轉(zhuǎn)換成json對(duì)象
<!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對(duì)象
$scope.data =angular.fromJson(localStorage.getItem('data'));
}]);
</script>
</body>
</html>
3.html字符串轉(zhuǎn)換成json對(duì)象
<!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對(duì)象
$scope.data = angular.fromJson('{"username":"泠泠在路上"}');
}]);
</script>
</body>
</html>
以上這篇angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲(chǔ)的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Angularjs中跨域設(shè)置白名單問(wèn)題
這篇文章主要介紹了Angularjs中關(guān)于跨域設(shè)置白名單問(wèn)題,需要的朋友可以參考下2018-04-04
簡(jiǎn)介AngularJS的視圖功能應(yīng)用
這篇文章主要介紹了AngularJS的視圖功能應(yīng)用,包括ng-view和ng-template以及$routeProvider的使用,以及 $routeProvider 需要的朋友可以參考下2015-06-06
AngularJS實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)庫(kù)信息
這篇文章主要為大家詳細(xì)介紹了AngularJS實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)庫(kù)信息效果的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
angularjs下ng-repeat點(diǎn)擊元素改變樣式的實(shí)現(xiàn)方法
今天小編就為大家分享一篇angularjs下ng-repeat點(diǎn)擊元素改變樣式的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
angularJs中ng-model-options設(shè)置數(shù)據(jù)同步的方法
今天小編就為大家分享一篇angularJs中ng-model-options設(shè)置數(shù)據(jù)同步的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Angular2.js實(shí)現(xiàn)表單驗(yàn)證詳解
這篇文章主要介紹了Angular2.js實(shí)現(xiàn)表單驗(yàn)證的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

