ionic 页面传递参数
1、使用AngularJS自带的$cacheFactory服务
$cacheFactory 从字面直译即为缓存工厂,可以用它来生成缓存对象,缓存对象以key-value的方式进行数据的存储,在整个应用内是单例的,可以在service或者controller中注入这个服务,然后就可以用它来自由的存取对象以及各种变量,下面是一个简单例子
- .controller('AppCtrl', function ($scope, $ionicModal, $timeout, $cacheFactory) {
- var user = {name: 'jax', age: 18, sex: '男'};
- var user_cache = $cacheFactory("user_cache"); //声明一个user_cache缓存对象
- user_cache.put("lol",user); //放入缓存对象
- .controller('PlaylistCtrl', function ($scope, $stateParams, $cacheFactory) {
- var user_cache = $cacheFactory.get("user_cache"); //取出名为user_cache的缓存对象
- var user = user_cache.get("lol"); //取出缓存对象中键值为lol的对象
- // user_cache.remove("lol"); //删除键值为lol对应的值
- // user_cache.removeAll(); //清除缓存对象中所有的键值对
- // user_cache.destroy(); //销毁user_cache缓存对象
- console.log(user);
- });
当从AppCtrl对应页面切换到PlaylistCtrl对应的页面时,浏览器控制台打印结果:
$cacheFactory常用的几个方位api如下:
- {{*}}
get({string} key)
— 返回与key
对应的value
值,如果未命中则返回undefined
。
- {void}
remove({string} key)
— 从缓存中删除一个键值对
- {void}
removeAll()
— 删除所有缓存中的数据
- {void}
destroy()
— 删除从$cacheFactory
引用的这个缓存.
- <ion-item href="#/app/playlists/{{playlist.id}}"> //playlists.html页面
- .controller('PlaylistCtrl', function ($scope, $stateParams, $cacheFactory) {
- var user_cache = $cacheFactory.get("user_cache"); //取出名为user_cache的缓存对象
- var user = user_cache.get("lol"); //取出缓存对象中键值为lol的对象
- // user_cache.remove("lol"); //删除键值为lol对应的值
- // user_cache.removeAll(); //清除缓存对象中所有的键值对
- // user_cache.destroy(); //销毁user_cache缓存对象
- console.log(user);
- var playlistId=$stateParams.playlistId; //用$stateParams 取值
- console.log("playlistId:"+playlistId);
- });
需要注意的是必须在app.js路由中配置接受这个参数
- .state('app.single', {
- url: '/playlists/:playlistId', //配置多个参数用:a/:b/:c
- views: {
- 'menuContent': {
- templateUrl: 'templates/playlist.html',
- controller: 'PlaylistCtrl'
- }
- }
3、service或者factory传值(service跟factory中都是单例模式,定义的变量在整个应用内唯一)
定义变量data
- angular.module('starter.controllers', [])
- .service('dataService',function () {
- var data="I come from service"; //定义变量
- return{
- getData:function () {
- return data;
- }
- }
- })
在controller中取出变量
- .controller('PlaylistCtrl', function ($scope, $stateParams, $cacheFactory,dataService) {
- console.log("sercice data:"+dataService.getData()); //得到data
- });
4、使用H5本地存储localStorage或者sessionStorage(还有indexDB,websql在数据量较大情况下使用)
getItem //取记录
setItem//设置记录
removeItem//移除记录
key//取key所对应的值
clear//清除记录
键值对存储,用法也是非常简单,上面给出了常用的api,
ionic 页面传递参数的更多相关文章
- JSP页面传递参数乱码问题整理
1.JSP页面之间传递中文参数乱码 (1).a.jsp中正常传递参数,b.jsp 中 <% String projectName = new String(request.getParamete ...
- 前端 使用localStorage 和 Cookie相结合的方式跨页面传递参数
A页面 html代码: 姓名:<input type="text" id="name1"> 年龄:<input type="text ...
- router-link跳转页面传递参数及页面刷新方法
使用router-link传参: 第一种: 路径:http://localhost:8080/goodListP?id=2 跳转的页面获取参数: this.$route.query.id 第二种: 路 ...
- Jquery Javascript 跳转页面传递参数以及获取url的参数
传递参数: window.location='editCourse.html?dataId='+dataId+''; 获取url中的参数(封装的方法): function getUrlParam ...
- jsp页面传递参数是如何与javabean进行关联的
总结:1.severlet容器是通过JavaBean中的属性方法名来获取属性名的,然后根据此属性名来从request中取值 2.JavaBean中属性方法的命名,set后的名称要与你从request中 ...
- SpringMVC 接受页面传递参数
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- Android 通过URL scheme 实现点击浏览器中的URL链接,启动特定的App,并调转页面传递参数
点击浏览器中的URL链接,启动特定的App. 首先做成HTML的页面,页面内容格式如下: <a href="[scheme]://[host]/[path]?[query]" ...
- SpringMVC 页面传递参数到controller的五种方式
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- React 列表页面传递参数
React 列表进入详情页面 首先安装 react-router-dom (4.0) npm/yarn install react-router-dom 路由跳转配置 列表 父组件 this.prop ...
随机推荐
- opencv学习之路(11)、图像几何变换
一.图像缩放 #include<opencv2/opencv.hpp> using namespace cv; void main(){ Mat src=imread("E:// ...
- 解决*.props打开失败问题
由于不同机器的绝对地址不一样,可能会出现解决*.props打开失败问题,解决方向如下: 1.找到这里缺失的.props文件,复制到固定路径下: 2.强行打开代码,这个时候是报错的 3.选择编辑 4.将 ...
- Windows 登录用户的类型
转载:http://blog.csdn.net/chenlycly/article/details/45419259 1.登录的是超级管理员Administrator 默认情况下,超级管理员Admin ...
- topcoder srm 435 div1
problem1 link 遍历未被删除的叶子结点即可. problem2 link 首先,将所有的蛋白质原子编号,设为$[0,m-1]$,每个原子可能对应多个长度为3的$ACGT$.设$n$为DNA ...
- topcoder srm 330 div1
problem1 link 直接模拟. import java.util.*; import java.math.*; import static java.lang.Math.*; public c ...
- Python3 tkinter基础 Entry show textvariable 密码输入框
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 《AngularJS开发下一代Web应用》读书笔记与感想
该书一共130页打算四天读完,边读边记录. 1. 2.学习MogoDB 3. 4. 5. 创建标识符的一段简单伪码模板: var myModule = angular.module(...); myM ...
- Mac 配置教程-日常篇
今年终于在推出 2018 款 MBP 时,看到升级了 CPU,我就果断下手「拔草」.本文记录使用 Mac 的一些配置,会长期更新. 为了控制文章的篇幅,我将 Mac 使用配置分成了两篇: Mac 配置 ...
- centos 编译lantrn
github上的安装指导: Custom fork of Go is currently required. We'll eventually switch to Go 1.7 which suppo ...
- 【NOIP 2016】Day1 T2 天天爱跑步
Problem Description 小 C 同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任 ...