[AngularJS] app.run($templateCache) -- zippy simple example

功能》点击title, 可以切换Content的显示/隐藏

复制代码
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>templateCache</title>
    <link href="foundation.min.css" rel="stylesheet"/>
    <script src="angular.min.js"></script>
    <script src="app.js"></script>
</head>
<body ng-app="app">
    <input type="text" ng-model="zippy.title" />
    <input type="text" ng-model="zippy.content" />
    <zippy title="{{zippy.title}}">
        This is content : {{zippy.content}}
    </zippy>
</body>
</html>
复制代码
复制代码
var app = angular.module("app", []);

app.run(function($templateCache) {
  $templateCache.put("zippy.html", '<div><h3 ng-click="toggleContent()">{{title}}</h3><div ng-show="isContentVisible" ng-transclude></div></div>');
});

app.directive('zippy',function(){
    return{
        restrict: 'E',
        scope:{title: '@'},
        transclude: true,
        templateUrl: 'zippy.html',
        link:function(scope){
            scope.isContentVisible = false;
            scope.toggleContent = function(){
                scope.isContentVisible = !scope.isContentVisible;
            }
        }
    }
});
复制代码

run会在所有angular models加载完毕后加载,适合在里面定义一些tempalte.

posted @   Zhentiw  阅读(437)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示