功能》點擊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