Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makes Angular JS works offline

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 question and ask it again

THE QUESTION: What I need to do is to make my web app enabled to work offline for this purpose the html files which are rendered as view (for example home.html) should be included somehow in the index.html, So when clicking on the some links there should be no need to have access to a html file instead a part of the same page for example a dive will be rendered, what modifications should I make to project to get this done

at the moment I made different html files and use them as templates when rendering views, the structure of app is like this :

  - index.html          
   - pages              
    ----- home.html
    ----- profile.html

here is the code for config the routes and controllers and views

  var formApp = angular.module('formApp', ['ngRoute']);

  formApp.config(function($routeProvider) {
    $routeProvider
       .when('/', {
           templateUrl : 'main',
           controller  : 'mainController'
        })
    .when('/profile', {
        templateUrl : 'profile',
        controller  : 'profileController'
    })
  }); 

And and my main.html file for example is like this :

 <div class="jumbotron text-center">
     <h1>Main Page</h1>

     <p>{{ message }}</p>
  </div>

َand somewhere in the index.html I have

         <div ng-view>
              {{ message }}
          </div>

The code works properly and everything is fine at the moment

like image 573
Siavosh Avatar asked May 14 '14 10:05

Siavosh


People also ask

Can Angular work offline?

To take your apps offline, you first need to add a service worker. At its core, a service worker is a Javascript file that intercepts and caches requests. The cool thing about it is that it runs separate from the main thread, so it can get in touch with a server while the app is not running.

Does AngularJS need a Web server?

Running the Development Web Server While AngularJS applications are purely client-side code, and it is possible to open them in a web browser directly from the file system, it is better to serve them from an HTTP web server.

Is Google abandoning Angular?

In January of 2018 we laid out our plans for the final releases of AngularJS before entering long-term support and last year, we extended the LTS due to the global pandemic until December 31, 2021. Well, friends, the time has come and we're no longer supporting AngularJS.

Is Angular only for web?

The term cross-platform in the Angular context refers to the following platforms: web, server, desktop, and mobile. Angular can run natively only on the web because it is a JavaScript framework.


2 Answers

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.

like image 130
Konstantin Krass Avatar answered Oct 11 '22 22:10

Konstantin Krass


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

like image 43
Rohan Büchner Avatar answered Oct 12 '22 00:10

Rohan Büchner