Makes Angular JS works offline

后端 未结 2 940
孤城傲影
孤城傲影 2020-12-29 14:59

I am developing a single page application, with help of AngularJS and I\'m new to it I asked the same question before but haven\'t got any answer so I am rephrasing my quest

相关标签:
2条回答
  • 2020-12-29 15:26

    This might not be 100% applicable to you. Depending on the solution & or platform you're using... But I've got a prototype application that I'm working on currently, built in Angular and Node.

    Although this was also my fist attempt at something like this... EG caching all the pages upfront. This seems to work quite well.

    All my pages get converted to a cache friendly format during the build phase. But in my solution, they are still regular html pages.

    home.tpl.html

    <div class="well home-menu">
        HOME
    </div>
    

    templates.js

    angular.module('templates', ['home.tpl.html']);
    
    angular.module("home.tpl.html", []).run(["$templateCache", function($templateCache) {
        $templateCache.put("home.tpl.html",
          "<div class=\"well home-menu\">\n" +
             "HOME\n"+
          "</div>");
     }]);
    

    controller

    angular.module('myApp.home', ['templates'])
    .config(function ($stateProvider) {
      $stateProvider
        .state('app.home', {
          url: '/home',
          templateUrl: 'home.tpl.html',
          controller: 'HomeController'
        });
    })
    .controller('HomeController', function ($scope) {
        //do something
    });
    

    All this magic courtesy of html2js

    grunt.loadNpmTasks('grunt-html2js');

    ...I do believe its possible to achieve this effect in various other ways that doesn't require grunt. For example manually creating the templates in the js file... but I wouldn't dream of recommending that route, as it could turn into a nightmare quickly

    0 讨论(0)
  • 2020-12-29 15:33

    To make your application work offline, you have to cache every file with the html5 cache manifest. Even the .html files, images, css, everything...

    The native "old" caching won't work here, because it still requires to communicate with the server to have the "304 Not Modified" http code.

    The manifest removes this step and doesn't even ask the server for the resources.

    An example manifest:

    CACHE MANIFEST
    /angular.js
    /index.html
    /page/home.html
    /page/profile.html
    NETWORK: 
    *
    

    How to include and use cache manifest check: http://www.w3schools.com/html/html5_app_cache.asp

    For debugging:

    Clearing a app cache under chrome enter url "chrome://appcache-internals/"


    EDIT: Due to comment and off the topic

    Instead of placing the html code in many own html files, you can include them in index.html like this:

    <script type="text/ng-template" id="one.html">
         <div>This is first template</div>
    </script>
    

    Then your templateURL is "one.html" without subpath.

    Check docs: https://docs.angularjs.org/api/ng/directive/script

    Hint:

    You dont need to place any paths there. During rendering phase, angularjs will store every html file in the $templateCache under it's id placed in those script elements.

    0 讨论(0)
提交回复
热议问题