ANGULARJS provides multiple template loading

Source: Internet
Author: User
Tags script tag



ANGULARJS provides a variety of template loading scenarios. The basics are obtained by means of a predefined path, via Ajax. Use a GULP-HTML2JS build tool to convert HTML templates to JS files. Introduced using the script tag.






In general, the first way to develop is to use the second way when deploying, and not to adopt a third way. This article briefly describes the introduction of tags into the template. The label type supported by Angularjs itself is text/ng-template and is now supported by another type:text/template.






A related blog post: https://www.zybuluo.com/bornkiller/note/6023 code Implementation






As explained in the previous blog post, the template in the $templateCache has the highest priority, so it needs to be used. The angularjs itself takes the form of script instruction to implement.




var scriptdirective = [' $templateCache ', function ($templateCache) {return

 {

   restrict: ' E ',

   terminal:true ,

   compile:function (element, attr) {

     if (Attr.type = = ' Text/ng-template ') {

       var templateurl = attr.id,

           Text = Element[0].text;

       $templateCache. Put (templateurl, text);}}};





The code is very simple, the decision type---write to the template. Encapsulation is completely invisible to the internal implementation, so it will be implemented in a personal way to understand.




<!DOCTYPE html>

<html>

<head lang="en">

   <meta charset="UTF-8">

   <title>Inline Template</title>

   <script type="text/template" id="love">

       <h3>love is color blind</h3>

       <p>why so serious about the world, behind the darkness</p>

   </script>

   <script src="libs/angular.min.js"></script>

   <script src="libs/angular-sanitize.min.js"></script>

   <script src="js/template.js"></script>

</head>

<body ng-app="template">

   <article ng-controller="TemplateCtrl">

       <div ng-bind-html="story"></div>

   </article>

</body>

</html>




Angular.module (' template ', [' ngsanitize '])

 . Run ([' $document ', ' $templateCache ', function ($document, $ Templatecache) {

   var scripts = Array.prototype.slice.call ($document [0].scripts, 0);

   Scripts.foreach (function (script) {

     if (Script.type = = ' Text/template ') {

       $templateCache. put (Script.id, script.innerhtml);}})

 . Controller (' Templatectrl ', [' $scope ', ' $templateCache ', ' $log ', function ($scope, $templateCache, $log) {

   $ Scope.story = $templateCache. Get (' love ');

 }];





The code is very simple, that is, by document.scripts so close to the original way to get the corresponding tag, and then write the contents of the tag inside the $templatecache.











Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.