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

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

<!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.html

相关文章
相关标签/搜索