【发布时间】:2018-12-21 12:16:00
【问题描述】:
我现在面临的问题是JSON.parse(localStorage.getItem('user_info')));之后
我怎样才能把这个字符串放在$scope对象中?
请为我的问题提出一些建议。
提前致谢
【问题讨论】:
我现在面临的问题是JSON.parse(localStorage.getItem('user_info')));之后
我怎样才能把这个字符串放在$scope对象中?
请为我的问题提出一些建议。
提前致谢
【问题讨论】:
您需要定义一个 AngularJS controller,然后使用 ng-controller 指令将其绑定到您的 Ionic 视图。下面是一个示例——我建议在 play.ionic.io 上使用它。
js
angular.module('app', ['ionic']).controller('MyController', function($scope) {
// localStorage.setItem('user_info', JSON.stringify('Hello World'));
$scope.userInfo = JSON.parse(localStorage.getItem('user_info'));
});
html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<link href="https://code.ionicframework.com/1.0.0/css/ionic.min.css" rel="stylesheet">
<script src="https://code.ionicframework.com/1.0.0/js/ionic.bundle.js"></script>
</head>
<body ng-app="app">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">LocalStorage Example</h1>
</ion-header-bar>
<ion-content class="padding" ng-controller="MyController">
<h1>{{userInfo}}</h1>
</ion-content>
</ion-pane>
</body>
</html>
【讨论】:
angular.module('app', ['ionic']).controller('MyController', function($scope) {
// localStorage.setItem('user_info', JSON.stringify('Hello World')); $scope.userInfo = JSON.parse(localStorage.getItem('user_info'));
});
【讨论】: