I'm trying to user Angular cookies in my controller but for some reason it is always undefined:
MyApp.controller('ValidationController', ["$cookies", function($cookies) {
debugger; //checking $cookies here return an object
$cookies.put("test","test"); //trying to add value to the cookie here return TypeError: undefined is not a function
alert(JSON.stringify($cookies));
}]);
var MyApp = angular.module("MyApp", [
"ui.router",
"ui.bootstrap",
"oc.lazyLoad",
"ngSanitize",
"ngCookies"
]);
This is the full stack error:
TypeError: undefined is not a function
at new <anonymous> (CookiesController.js:6)
at Object.e [as invoke] (angular.min.js:37)
at $get.z.instance (angular.min.js:76)
at angular.min.js:59
at s (angular.min.js:7)
at v (angular.min.js:59)
at g (angular.min.js:52)
at angular.min.js:51
at angular.min.js:17
at l.$get.l.$eval (angular.min.js:126)
any ideas?
Seems like $cookies doesn't have setter in angular 1.3.10 while angular 1.4.0 does provided, Please refer their documents.
You could solve your problem by 2 ways.
If you want to stay with angular 1.3.10 then use $cookieStore object instead of $cookies
Do upgrade angular to version 1.4.0.beta.6 latest version
Hope this could help you, Thanks.
Related
I use Angularjs 1.2.21 in my project.
I download and install angular-route.min.js file
Here is main module defenition:
angular.module('dashboard', ['ngRoute','layers'])
.config(function () {});
In console I get this 2 errors:
Uncaught TypeError: Cannot read property 'w' of null
at initialize.setMap (OpenLayers.js:625)
at initialize.addLayer (OpenLayers.js:182)
at initialize.addLayers (OpenLayers.js:182)
at initialize (OpenLayers.js:177)
at mapApp.js:205
at HTMLDocument.<anonymous> (mapApp.js:221)
at fire (jquery-1.8.3.js:974)
at Object.fireWith [as resolveWith] (jquery-1.8.3.js:1084)
at Function.ready (jquery-1.8.3.js:406)
at HTMLDocument.DOMContentLoaded (jquery-1.8.3.js:83)
And second error is this:
angular.min.js:92 TypeError: Cannot read property 'charAt' of undefined
at Tb.$$parse (angular.min.js:86)
at angular.min.js:90
at k.$eval (angular.min.js:111)
at k.$digest (angular.min.js:108)
at angular.min.js:90
at angular.min.js:38
at q (angular.min.js:7)
at g (angular.min.js:38)
at angular.min.js:31
at q (angular.min.js:7)
When I remove ngRoute from dependency injection everything works fine:
angular.module('dashboard', ['layers'])
.config(function () {});
Any idea why I get errors above?
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.
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'm trying to set up a unit testing environment for my project and I'm having some problems. I'm trying to use Testacular with Jasmine for testing my AngularJS code.
I have a module Services which has a factory method called KeepAlive. Here is its set up.
angular.module('services', []).factory('KeepAlive', ['$rootScope', function($rootScope){
//My Code
}]);
This module is attached to a module called MainModule. Here is its code.
angular.module('MainModule', ['filters', 'services', 'directives', 'ui'])
.config(['$httpProvider', function($httpProvider){
//My Code
}]).run(['$rootScope', '$timeout', '$routeParams', 'KeepAlive', function($rootScope, $timeout, $routeParams, KeepAlive){
//My Code
}]);
So Now I want to add this code for my tests. So here is my testacular file set up.
JASMINE,
JASMINE_ADAPTER,
'assets/lib/jquery/jquery-1.7.1.js',
'assets/lib/angular/angular.js',
'assets/lib/angular/angular-ui.js',
'jsTests/MockingLib/angular-mocks.js',
'assets/scripts/modules/Admin.js',
'assets/scripts/modules/MainModule.js',
'assets/scripts/services/KeepAlive.js',
'assets/scripts/services/Admin.js',
'assets/scripts/filters.js',
'assets/scripts/directives.js',
'assets/scripts/controllers/admin/*.js',
Now this is throwing a error in my testacular console. Here is the error that I'm getting.
Error: Unknown provider: KeepAliveProvider <- KeepAlive
at Error (<anonymous>)
at d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2627:8
at Object.getService [as get] (d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2755:32)
at d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2632:38
at getService (d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2755:32)
at Object.invoke (d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2773:6)
at d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2637:71
at Array.forEach (native)
at forEach (d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:110:11)
at Object.createInjector [as injector] (d:/Work/Workspace/TalentNetwork/assets/lib/angular/angular.js:2637:3)
Here is my Jasmine Test case.
beforeEach(function () {
admin = module("MainModule");
module("services");
});
What I'm I missing? I'm new to testacular. Thanks in advance.
The error says that somebody asked for a service called "KeepAlive" but the injector does not know how to create it (there is no provider for it).
That means, you probably didn't load the module.
When you bootstrap your app on production, you give it ng-app="something", which tells Angular which module to load. In the same way, you need to specify which modules you wanna load during testing.
This should solve your problem:
beforeEach(module('services'));