2

I am new to angular js and following this tutorial http://embed.plnkr.co/dd8Nk9PDFotCQu4yrnDg/ for creating simple SPA page. In which, When I use Local and Session storage concepts in About page ,It is perfectly working in Firefox,IE except Chrome. I am not able to find the problem in Chrome. So Please need your help.

index.html

<html ng-app="scotchApp">
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>
        <script type="text/javascript" src="https://cdn.jsdelivr.net/ngstorage/0.3.6/ngStorage.min.js"></script>
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
   <script src="./script.js" type="text/javascript"></script>
    </head>
    <body ng-controller="mainController">
    <header>
            <nav class="navbar navbar-default">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" href="/">Angular Routing Example</a>
                </div>

                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
                    <li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
                    <li><a href="#contact"><i class="fa fa-comment"></i> Contact</a></li>
                </ul>
            </div>
            </nav>
        </header>

        <!-- MAIN CONTENT AND INJECTED VIEWS -->
        <div id="main">

           <div ng-view></div>

        </div>

    </body>
    </html>

pages/about.html:

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

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

         <input type="button" value="Save" ng-click="Save()" />
         <input type="button" value="Get" ng-click="Get()" />

    </div>

pages/contact.html:

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

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

pages/home.html:

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

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

scripts.js:

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


    scotchApp.config(function($routeProvider) {
        $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : 'pages/home.html',
                controller  : 'mainController'
            })

            // route for the about page
            .when('/about', {
                templateUrl : 'pages/about.html',
                controller  : 'aboutController'
            })

            // route for the contact page
            .when('/contact', {
                templateUrl : 'pages/contact.html',
                controller  : 'contactController'
            });
    });

    // create the controller and inject Angular's $scope
    scotchApp.controller('mainController', function($scope, $localStorage, $sessionStorage, $window) {
        // create a message to display in our view
        $scope.message = 'Everyone come and see how good I look!';
    });

    scotchApp.controller('aboutController', function($scope, $localStorage, $sessionStorage, $window) {
        $scope.message = 'Look! I am an about page.';
        $scope.Save = function () {
                $localStorage.LocalMessage = "LocalStorage: My name is Mudassar Khan.";
                $sessionStorage.SessionMessage = "SessionStorage: My name is Mudassar Khan.";
                $localStorage.$save();
                $sessionStorage.$save();
            }
            $scope.Get = function () {
                $window.alert($localStorage.LocalMessage + "\n" + $sessionStorage.SessionMessage);
            }
    });

    scotchApp.controller('contactController', function($scope, $localStorage, $sessionStorage, $window) {
        $scope.message = 'Contact us! JK. This is just a demo.';
    });

Thanks In advance.

EverGreen
  • 217
  • 1
  • 12
  • Is there any error console? – Asim K T Mar 01 '16 at 06:22
  • @Asim K T XMLHttpRequest cannot load file:///C:/Users/sivakumarj/Desktop/Examples/Session/angular/pages/home.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. – EverGreen Mar 01 '16 at 06:30
  • Looks like you have to use a local server to serve files. http://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local. Or start chrome in terminal with this flag: --allow-file-access-from-files – Asim K T Mar 01 '16 at 06:39
  • It can't request direct files using that kind of request you need to have them served from somewhere. If you need something easy node.js works well for serving localhost and some resources can get you up and running in minutes. – Binvention Mar 01 '16 at 06:48

1 Answers1

1

you need to serve your html via a local server . Now Your files are being served as file:/// it should be http://

refer this stackoverflow answer

Community
  • 1
  • 1
Rishabh Jain
  • 526
  • 1
  • 10
  • 26