1、使用AngularJS自带的$cacheFactory服务

$cacheFactory 从字面直译即为缓存工厂,可以用它来生成缓存对象,缓存对象以key-value的方式进行数据的存储,在整个应用内是单例的,可以在service或者controller中注入这个服务,然后就可以用它来自由的存取对象以及各种变量,下面是一个简单例子

  1. .controller('AppCtrl', function ($scope, $ionicModal, $timeout, $cacheFactory) {
  2. var user = {name: 'jax', age: 18, sex: '男'};
  3. var user_cache = $cacheFactory("user_cache");  //声明一个user_cache缓存对象
  4. user_cache.put("lol",user);    //放入缓存对象
  1. .controller('PlaylistCtrl', function ($scope, $stateParams, $cacheFactory) {
  2. var user_cache = $cacheFactory.get("user_cache");   //取出名为user_cache的缓存对象
  3. var user = user_cache.get("lol");   //取出缓存对象中键值为lol的对象
  4. // user_cache.remove("lol");  //删除键值为lol对应的值
  5. // user_cache.removeAll(); //清除缓存对象中所有的键值对
  6. // user_cache.destroy(); //销毁user_cache缓存对象
  7. console.log(user);
  8. });

当从AppCtrl对应页面切换到PlaylistCtrl对应的页面时,浏览器控制台打印结果:

$cacheFactory常用的几个方位api如下:

- {{*}} get({string} key) — 返回与key对应的value值,如果未命中则返回undefined
- {void} remove({string} key) — 从缓存中删除一个键值对
- {void} removeAll() — 删除所有缓存中的数据
- {void} destroy() — 删除从$cacheFactory引用的这个缓存.

2、使用url传参
例:playlists.htm页面将 playlist.id 传递到 playlist页面
  1. <ion-item  href="#/app/playlists/{{playlist.id}}">  //playlists.html页面
  1. .controller('PlaylistCtrl', function ($scope, $stateParams, $cacheFactory) {
  2. var user_cache = $cacheFactory.get("user_cache");   //取出名为user_cache的缓存对象
  3. var user = user_cache.get("lol");   //取出缓存对象中键值为lol的对象
  4. // user_cache.remove("lol");  //删除键值为lol对应的值
  5. // user_cache.removeAll(); //清除缓存对象中所有的键值对
  6. // user_cache.destroy(); //销毁user_cache缓存对象
  7. console.log(user);
  8. var playlistId=$stateParams.playlistId;  //用$stateParams 取值
  9. console.log("playlistId:"+playlistId);
  10. });

需要注意的是必须在app.js路由中配置接受这个参数

  1. .state('app.single', {
  2. url: '/playlists/:playlistId',  //配置多个参数用:a/:b/:c
  3. views: {
  4. 'menuContent': {
  5. templateUrl: 'templates/playlist.html',
  6. controller: 'PlaylistCtrl'
  7. }
  8. }

3、service或者factory传值(service跟factory中都是单例模式,定义的变量在整个应用内唯一)

定义变量data

  1. angular.module('starter.controllers', [])
  2. .service('dataService',function () {
  3. var data="I come from service";  //定义变量
  4. return{
  5. getData:function () {
  6. return data;
  7. }
  8. }
  9. })

在controller中取出变量

  1. .controller('PlaylistCtrl', function ($scope, $stateParams, $cacheFactory,dataService) {
  2. console.log("sercice data:"+dataService.getData());  //得到data
  3. });


4、使用H5本地存储localStorage或者sessionStorage(还有indexDB,websql在数据量较大情况下使用)

getItem //取记录

setItem//设置记录

removeItem//移除记录

key//取key所对应的值

clear//清除记录

键值对存储,用法也是非常简单,上面给出了常用的api,

ionic 页面传递参数的更多相关文章

  1. JSP页面传递参数乱码问题整理

    1.JSP页面之间传递中文参数乱码 (1).a.jsp中正常传递参数,b.jsp 中 <% String projectName = new String(request.getParamete ...

  2. 前端 使用localStorage 和 Cookie相结合的方式跨页面传递参数

    A页面 html代码: 姓名:<input type="text" id="name1"> 年龄:<input type="text ...

  3. router-link跳转页面传递参数及页面刷新方法

    使用router-link传参: 第一种: 路径:http://localhost:8080/goodListP?id=2 跳转的页面获取参数: this.$route.query.id 第二种: 路 ...

  4. Jquery Javascript 跳转页面传递参数以及获取url的参数

    传递参数: window.location='editCourse.html?dataId='+dataId+''; 获取url中的参数(封装的方法):    function getUrlParam ...

  5. jsp页面传递参数是如何与javabean进行关联的

    总结:1.severlet容器是通过JavaBean中的属性方法名来获取属性名的,然后根据此属性名来从request中取值 2.JavaBean中属性方法的命名,set后的名称要与你从request中 ...

  6. SpringMVC 接受页面传递参数

    一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public  String login (String username,String password)   : 解 ...

  7. Android 通过URL scheme 实现点击浏览器中的URL链接,启动特定的App,并调转页面传递参数

    点击浏览器中的URL链接,启动特定的App. 首先做成HTML的页面,页面内容格式如下: <a href="[scheme]://[host]/[path]?[query]" ...

  8. SpringMVC 页面传递参数到controller的五种方式

    一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public  String login (String username,String password)   : 解 ...

  9. React 列表页面传递参数

    React 列表进入详情页面 首先安装 react-router-dom (4.0) npm/yarn install react-router-dom 路由跳转配置 列表 父组件 this.prop ...

随机推荐

  1. netty集成ssl完整参考指南(含完整源码)

    虽然我们在内部rpc通信中使用的是基于认证和报文头加密的方式实现安全性,但是有些时候仍然需要使用SSL加密,可能是因为对接的三方系统需要,也可能是由于open的考虑.中午特地测了下netty下集成ss ...

  2. Zookeeper注册中心的搭建

    一.Zookeeper的介绍 Zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用 ...

  3. Kali系列之multi/handler(渗透win7)

    环境 靶机 192.168.137.133 kali 192.168.137.135 步骤+ 生成后门 msfvenom -p windows/meterpreter/reverse_tcp LHOS ...

  4. 学习dart从这里开始

    void main() { ; i < ; i++) { print('hello ${i + 1}'); } } // 定义个方法. printNumber(num aNumber) { pr ...

  5. vim插件的安装方式 -- vim注释插件和doxygen函数注释生成插件-ctrlp插件-tabular等号对齐 插件

    使用unzip的时候 指定 -d选项, 是说明解压到的 目标地址. 这个参数还是比较方便的, 比直接unzip到当前目录, 然后在去拷贝到目标目录, 然后再删除当前目录中的解压文件夹, 方便多了. 使 ...

  6. luoguP4072 [SDOI2016]征途

    [SDOI2016]征途 大体 大概就是推推公式,发现很傻逼的\(n^3\)DP get60 进一步我们发现状态不能入手,考虑优化转移 套个斜率优化板子 每一层转移来一次斜率优化 思路 先便便式子 \ ...

  7. 集合04_Set

    Set集合总览 集合元素无序.不重复,三个实现类都是线程不安全的,最好在创建时通过Collections工具类的synchronizedSortedSet方法来包装Set集合,防止对set集合的意外非 ...

  8. Perceptual Losses for Real-Time Style Transfer and Super-Resolution and Super-Resolution 论文笔记

    Perceptual Losses for Real-Time Style Transfer and Super-Resolution and Super-Resolution 论文笔记 ECCV 2 ...

  9. C#winform的textbox怎么设置滚动条

    用 C#开发软件的时候文本框textbox是没有滚动条的,而且是单行文本.下面教大家如何设置多行,并且设置横向滚动条和垂直滚动条. 打开VS工具,创建一个winform窗体项目.系统会自动创建一个主窗 ...

  10. Using git-flow to automate your git branching workflow

    Using git-flow to automate your git branching workflow Vincent Driessen’s branching model is a git b ...