angularjs1-8,cacheFactory,sce

本文介绍如何使用AngularJS中的$cacheFactory进行数据缓存,并演示了如何利用$sce服务安全地进行HTML内容的数据绑定,包括从远程API获取HTML内容并显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

复制代码
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
#parent div{ width:300px; height:500px; border:1px #000 solid; margin:20px;}
#parent ul{ width:200px; position:fixed; top:0; right:0;}
</style>
<script src="angular.min.js"></script>
<script>

var m1 = angular.module('myApp',[]);
m1.controller('Aaa',['$scope','$cacheFactory',function($scope,$cacheFactory){
//localstorage写入永久缓存,sessionstorage浏览器不关闭就有,cacheFactory只要put不写就get不到了,cacheFactory只是用于多个controller临时数据共享,没有写入storage。多个controller临时数据共享也可以通过自定义服务来实现。
    var cache=$cacheFactory('cacheId');
    cache.put('name','张三');
    cache.put('age','20');
    var name=cache.get('name');
    console.log(name);
}]);
m1.controller('Bbb',['$scope','$cacheFactory',function($scope,$cacheFactory){
    var cache=$cacheFactory.get('cacheId');
    var name=cache.get('name');
    var age=cache.get('age');
    console.log(name);
    console.log(age);
}]);

</script>
</head>
<body>
<div ng-controller="Aaa">
</div>
<div ng-controller="Bbb">
</div>
</body>
</html>
复制代码
复制代码
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript" src="angular.min.js"></script>
</head>
<body>
<div ng-app="myApp">
    <div ng-controller="firstController">
        {{name}}
        <div ng-bind-html="text"></div>
        <div ng-bind-html="detailContent()"></div>
            1111111111
        <div ng-bind-html="portalDetail"></div>
    </div>
</div>
<script type="text/javascript">
    var app = angular.module("myApp", []);
    app.controller('firstController',function($scope,$timeout,$sce,$http){//依赖注入$sce
        $scope.name = 'hello';
        //sce服务用于解析html,
        $scope.text = $sce.trustAsHtml('<h1>hello text</h1>');
        var myUrl = "http://www.phonegap100.com/appapi.php?a=getPortalArticle&aid=338&callback=JSON_CALLBACK";
        $http.jsonp(myUrl).success(
                function(data){
                    $scope.portalDetail = $sce.trustAsHtml(data.result[0].content);
                    $scope.detailContent = function() {
                        return $sce.trustAsHtml(data.result[0].content);
                    };
                }
        ).error(function(){
                    alert('失败');
        });
    });
</script>
</body>
</html>
复制代码

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/7240962.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值