I am new to angularjs, I have an issue that Data is disappear as page got refreshed, i search and find a similar question posted in stackoverflow, that i referred it Data will disappear after page refresh,
my factory service
app.factory("MoreInfoOnChallengeSvc", ["$window", function ($window) {
var data = localStorage.getItem("data")? JSON.parse(localStorage.getItem("data")) || {};
function setChallengeId(id){
data = id;
localStorage.setItem("data", JSON.stringify(data));
}
function getData(){
return data;
}
return{
setChallengeId: setChallengeId,
getData: getData
};
}]);
error :
EarnfitApp.js:240 Uncaught SyntaxError: Unexpected token ;
jquery-migrate-1.1.0.min.js:1'//# sourceURL' and '//# sourceMappingURL' are deprecated, please use '//# sourceURL=' and '//# sourceMappingURL=' instead.
angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module earnfitApp due to:
Error: [$injector:nomod] Module 'earnfitApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.5.8/$injector/nomod?p0=earnfitApp
at http://localhost:2000/earnfitangular/angular/assets/js/angular.js:68:12
at http://localhost:2000/earnfitangular/angular/assets/js/angular.js:2082:17
at ensure (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:2006:38)
at module (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:2080:14)
at http://localhost:2000/earnfitangular/angular/assets/js/angular.js:4617:22
at forEach (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:321:20)
at loadModules (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:4601:5)
at createInjector (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:4523:19)
at doBootstrap (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:1758:20)
at bootstrap (http://localhost:2000/earnfitangular/angular/assets/js/angular.js:1779:12)
http://errors.angularjs.org/1.5.8/$injector/modulerr?p0=earnfitApp&p1=Error…t%3A2000%2Fearnfitangular%2Fangular%2Fassets%2Fjs%2Fangular.js%3A1779%3A12)
I don't know what i did wrong
You used the ternary operator the wrong way. You can use it like this:
var data = localStorage.getItem("data") ? JSON.parse(localStorage.getItem("data")) : {};
Or keep the || pattern without ternary operator:
var data = JSON.parse(localStorage.getItem("data")) || {};
This will work because JSON.parse(null) returns null.
Related
I am getting a few different errors. One of them is the one listed in the title.
This failure is for my TEST file that is written using Karma.
I'm assuming the problem is related to $httpBackend not registering somewhere. I am building an application using IONIC, Angular, and Rails.
I've tried using both _$httpBackend_ syntax and the $httpBackend syntax in the beforeEach(inject(function()) and I'm still getting the same error.
Here is the full error:
BillService
✗ fetches a list of Bills
Error: [$injector:modulerr] Failed to instantiate module starter due to:
Error: [$injector:modulerr] Failed to instantiate module ionic due to:
Error: [$injector:nomod] Module 'ionic' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.5.3/$injector/nomod?p0=ionic
at /Users/knowerlittle/Dropbox/Projects/splitter-frontend/www/lib/ionic/js/angular/angular.js:68:12
TypeError: Cannot read property 'expectGET' of undefined
at Object.<anonymous> (/Users/knowerlittle/Dropbox/Projects/splitter-frontend/test/unit/BillService.spec.js:15:16)
BELOW IS THE TEST FILE:
describe('BillService', function() {
beforeEach(module('starter'));
var billService, httpBackend;
var billData = [{event: "Party"}, {event: "Birthday"}];
beforeEach(inject(function(_billService_, _$httpBackend_) {
billService = _billService_;
httpBackend = _$httpBackend_;
}));
it('fetches a list of Bills', function(){
httpBackend.expectGET("http://localhost:3000/bills/").respond(billData);
var bill1 = { event: "Party"};
var bill2 = { event: "Birthday"};
billService.getAll().then(function(bills) {
expect(bills).toEqual([bill1, bill2]);
});
httpBackend.flush();
});
});
Any help would be amazing! Thank you in advance.
I have this piece of code written and for some reason after I made some changed it complains within data obj css property.
The error says that the concat is not defined, even thou I have it on my packages.json defined.
.state('signup-facebook', {
url: '/students/signup',
templateUrl: MODULE_PATH + 'become/authentication/signup/connect-facebook.client.view.html',
controller: 'StudentAuthenticationController',
controllerAs: 'StudentAuthentication',
data: {
step: 1,
animate: true,
css: cssModules.students.concat(cssModules.users), // This line of code showing error
cssModuleName: 'students'
}
})
I just dont understand why this is not working.
Here you have the full error that Chrome displays:
Uncaught Error: [$injector:modulerr] Failed to instantiate module
coderstrust due to: Error: [$injector:modulerr] Failed to instantiate
module students due to: TypeError: Cannot read property 'concat' of
undefined
at config (http://localhost:3000/modules/students/client/config/students.become.client.routes.js:23:45)
at Object.invoke (http://localhost:3000/lib/angular/angular.js:4708:19)
at runInvokeQueue (http://localhost:3000/lib/angular/angular.js:4601:35)
at http://localhost:3000/lib/angular/angular.js:4610:11
at forEach (http://localhost:3000/lib/angular/angular.js:322:20)
at loadModules (http://localhost:3000/lib/angular/angular.js:4591:5).....
What is cssModules.students? The problem is not with concat. The problem is with cssModules.students which is undefined and when you will try to perform any method on undefined you will get an error.
Hope this helps :)
Happy Learning
I'm following a tutorial which is making Angular on Rails.
here is a JSFiddle URL for the project: https://jsfiddle.net/dcbavw4e/4/
I'm currently getting 2 errors right now:
1. Uncaught SyntaxError: Unexpected token .
Error occuring at : .controller('MainCtrl', ['$scope', 'posts', function($scope, posts) {
2. angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module flapperNews due to:
Error: [$injector:nomod] Module 'flapperNews' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.4.9/$injector/nomod?p0=flapperNews
at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:68:12
at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:2006:17
at ensure (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:1930:38)
at module (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:2004:14)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:4447:22
at forEach (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:341:20)
at loadModules (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:4431:5)
at createInjector (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:4356:11)
at doBootstrap (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:1677:20)
at bootstrap (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:1698:12)
http://errors.angularjs.org/1.4.9/$injector/modulerr?p0=flapperNews&p1=Erro….googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.4.9%2Fangular.js%3A1698%3A12)
I'm not really sure what I'm doing wrong since I did check App name in index.html and app.js but they all match.
There were some syntax errors in your code. At line 24 on the fiddle, you used a ';' it breaks the chain, so controller 'MainCtrl' won't get attached to module 'flapperNews'. (There are a few more mistakes as well.) See the fiddle in comments
.factory('posts', [function(){
var o = {
posts: []
};
return o;
}]);
I'm getting an error while trying to unit test my ionic app. I went through previously asked questions and non of the answers worked for me. Any ideas what I'm doing wrong?
Error I'm getting:
PhantomJS 1.9.8 (Mac OS X 0.0.0) StockCtrl should have a scope variable defined FAILED
TypeError: 'undefined' is not a function (evaluating 'queueableFn.fn.call(self.userContext)')
Error: [ng:areq] Argument 'StockCtrl' is not a function, got undefined
http://errors.angularjs.org/1.4.3/ng/areq?p0=StockCtrl&p1=not%20a%20function%2C%20got%20undefined
undefined
at assertArg (/Users/Projects/online-shop/www/lib/angular/angular.js:1770)
at assertArgFn (/Users/Projects/online-shop/www/lib/angular/angular.js:1781)
at /Users/Projects/online-shop/www/lib/angular/angular.js:8975
at /Users/Projects/online-shop/www/lib/angular-mocks/angular-mocks.js:1848
at /Users/Projects/online-shop/test/stockController.spec.js:9
at invoke (/Users/Projects/online-shop/www/lib/angular/angular.js:4450)
at workFn (/Users/Projects/online-shop/www/lib/angular-mocks/angular-mocks.js:2404)
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.006 secs / 0.007 secs)
stockFactory.js
app.factory('stockService', function($resource){
return $resource('js/shopStock.json/:items', 'items');
})
stockController.js
app.controller("StockCtrl", function($scope, $rootScope, $stateParams, stockService) {
var items = stockService.get(function(){
$scope.stock = items['items'];
});
});
stockController.spec.js
describe('StockCtrl', function() {
beforeEach(angular.module('Shop'));
var scope;
beforeEach(inject(function($rootScope, $controller){
scope = $rootScope.$new();
$controller("StockCtrl", {$scope: scope});
}));
it("should have a scope variable defined", function() {
expect(scope).toBeDefined();
});
});
I have tried'module' instead of 'angular.module' as suggested in one of the previously posted questions but end up with other error:
Error: [$injector:modulerr] Failed to instantiate module Shop due to:
Error: [$injector:modulerr] Failed to instantiate module ionic due to:
Error: [$injector:nomod] Module 'ionic' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.4.3/$injector/nomod?p0=ionic
I would be very grateful for a help.
Also, does anyone know how test $resource as a next step?
You definitely need to remove beforeEach(angular.module('Shop')) and replace it with beforeEach(module('Shop')). Your problem with the ionic module not being found could be a few things . I'd start by making sure Karma is loading the correct files into the browser when it runs your tests and that it's doing it in the right order. Check the files property in your Karma.conf file has your ionic module referenced.
I have the following code:
function configRestangular(RestangularProvider, BASE_URL) {
RestangularProvider.setBaseUrl(BASE_URL);
RestangularProvider.setDefaultHeaders({ 'Content-Type': 'application/json' });
RestangularProvider.setErrorInterceptor(
function (response, deferred) {
if (response.status === 401 || response.status === 403) {
//$state.go('login');
}
});
}
core.config(configRestangular);
When I pass $localStorage to function configRestangular(RestangularProvider, BASE_URL), application throws an exception.
Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module app.core due to:
Error: [$injector:unpr] Unknown provider: $localStorage
http://errors.angularjs.org/1.4.0-rc.2/$injector/unpr?p0=%24localStorage
I have registered the ngStorage module in main module. ngStorage.js file is also added in my main page. $localStorage service is working in all controllers except this config function.
I am using https://github.com/gsklee/ngStorage module for localStorage.
You also have to add 'ngStorage' and 'RestangularProvider' as a dependency for your application:
var app = angular.module('MyApp', ['ngStorage','RestangularProvider']);