angularJS学习---自定义指令---templateUrl的几种写法

第一种写法:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div ng-app="myApp">
  9. <script type="text/ng-template" id="zippy.html">
  10. <div>
  11. <h3 ng-click="toggleContent()">{{title}} </h3>
  12. <div ng-show="isContentVisiable" ng-transclude></div>
  13. </div>
  14. </script>
  15. <input type="text" ng-model="model.title" />
  16. <br />
  17. <input type="text" ng-model="model.content" />
  18. <zippy title="{{model.title}}">
  19. the content is :{{model.content}}
  20. </zippy>
  21. </div>
  22. <script type="text/javascript" src="../js/angular.min.js"></script>
  23. <script type="text/javascript" src="../js/templateUrl.js"></script>
  24. </body>
  25. </html>

第二种写法是把html放在单独的html文件里面:

  1. <div>
  2. <h3 ng-click="toggleContent()">{{title}} </h3>
  3. <div ng-show="isContentVisiable" ng-transclude></div>
  4. </div>

  1. var app = angular.module('myApp', []);
  2. //app.run(function($templateCache) {
  3. // $templateCache.put("zippy.html", " <div><h3 ng-click="toggleContent()">{{title}} </h3><div ng-show="isContentVisiable" ng-transclude></div></div>")
  4. //});
  5. app.directive('zippy',function ($templateCache) {
  6. //console.log($templateCache.get("zippy.html"));
  7. return {
  8. restrict:'E',
  9. transclude:true,
  10. scope:{
  11. title:'@'
  12. },
  13. templateUrl:'../node/templateUrl-zippy.html',
  14. //template: $templateCache.get("zippy.html");
  15. link: function(scope) {
  16. scope.isContentVisiable = false;
  17. scope.toggleContent = function() {
  18. scope.isContentVisiable = !scope.isContentVisiable;
  19. };
  20. }
  21. }
  22. })

不过这里因为js目录是当前的目录,你的html目录肯定在另外一个文件夹里,所以自己记得路径的问题


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div ng-app="myApp">
  9. <!--
  10. <script type="text/ng-template" id="zippy.html">
  11. <div>
  12. <h3 ng-click="toggleContent()">{{title}}</h3>
  13. <div ng-show="isContentVisiable" ng-transclude></div>
  14. </div>
  15. </script>
  16. -->
  17. <input type="text" ng-model="model.title" />
  18. <br />
  19. <input type="text" ng-model="model.content" />
  20. <zippy title="{{model.title}}">
  21. the content is :{{model.content}}
  22. </zippy>
  23. </div>
  24. <script type="text/javascript" src="../js/angular.min.js"></script>
  25. <script type="text/javascript" src="../js/templateUrl.js"></script>
  26. </body>
  27. </html>


这样做就使得阅读代码变得更加的容易


第三种写法:把模板放在运行块里面

  1. var app = angular.module('myApp', []);
  2. app.run(function($templateCache) {
  3. $templateCache.put("zippy.html", ' <div><h3 ng-click="toggleContent()">{{title}} </h3><div ng-show="isContentVisiable" ng-transclude></div></div>')
  4. });
  5. app.directive('zippy',function (/*$templateCache*/) {
  6. //console.log($templateCache.get("zippy.html"));
  7. return {
  8. restrict:'E',
  9. transclude:true,
  10. scope:{
  11. title:'@'
  12. },
  13. templateUrl:'zippy.html',
  14. //templateUrl:'../node/templateUrl-zippy.html',
  15. //template: $templateCache.get("zippy.html");
  16. link: function(scope) {
  17. scope.isContentVisiable = false;
  18. scope.toggleContent = function() {
  19. scope.isContentVisiable = !scope.isContentVisiable;
  20. };
  21. }
  22. }
  23. })


html文件:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div ng-app="myApp">
  9. <!--
  10. <script type="text/ng-template" id="zippy.html">
  11. <div>
  12. <h3 ng-click="toggleContent()">{{title}}</h3>
  13. <div ng-show="isContentVisiable" ng-transclude></div>
  14. </div>
  15. </script>
  16. -->
  17. <input type="text" ng-model="model.title" />
  18. <br />
  19. <input type="text" ng-model="model.content" />
  20. <zippy title="{{model.title}}">
  21. the content is :{{model.content}}
  22. </zippy>
  23. </div>
  24. <script type="text/javascript" src="../js/angular.min.js"></script>
  25. <script type="text/javascript" src="../js/templateUrl.js"></script>
  26. </body>
  27. </html>


建议还是使用第二种方法
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值