Try to search your question here, if you can't find : Ask Any Question Now ?

Application's html loads, but js function built with angular gets stuck loading indefinitely

HomeCategory: stackoverflowApplication's html loads, but js function built with angular gets stuck loading indefinitely
Avatarcraig asked 2 weeks ago

I’m working on an AngularJS application that reads and writes cookies and my javascript code is not working properly. My html loads fine, but when it comes to load the angular script it gets stuck loading indefinitely.

I’ve tried with a simple ‘hello world’ console log with a simple angular function and it works fine, which means the problem is not located on the angular script, but on the cookie function itself, although I can’t point out where and since the page’s not loading at all, I can’t see any output on the console.

Here’s my html code.

<!doctype html>
<html ng-app = 'cookieApp'>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <div ng-controller = 'MainController'>
            <label>Save cookie:</label>
            <input ng-model = 'value'>
            <button ng-click = 'saveCookie(value);'>Save</button>
            <h2>Get Cookie : {{getCookie()}}</h2>
            <script src = 'https://ajax.googleapis.com/ajax/libs/angularjs/1.2.32/angular.js'></script>
            <script src = 'app.js'></script>
        </div>
    </body>
</html>

Here’s the javascript code

angular.module('cookieApp', []).controller('MainController', ['$scope', 'cookie', function($scope, cookie){
    $scope.value = '';
    $scope.saveCookie = function(value){
        cookie.write('cap_value', value);
    }
    $scope.getCookie = function(){
        return cookie.read('cap_value');
    }
}]).
factory('cookie', [function(){
    return{
        read: function(name){
            var i, c, nameEQ = name + '=';
            var ca = document.cookie.split(';');
            for(i = 0; i < ca.length; i++){
                c = ca[i];
                while(c.charAt(0) == ''){
                    c = c.substring(1, c.length);
                }
                if(c.indexOf(nameEQ) == 0){
                    return c.substring(nameEQ.length, c.length);
                }
            }
            return '';
        },
        write: function(name, value){
            date = new Date();
            date.setTime(date.getTime() + (72*4));
            expires = '; expires = ' + date.toGMTString();
            document.cookie = name + '=' + value + expires + '; path = /';
        }
    }
}]);

I expect the application to save a cookie when I type an input and then print it in the <h2> tag.

1 Answers
Best Answer
AvatarFernando answered 2 weeks ago
Your Answer

0 + 15 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com