I have a controller as shown below.
(function () {
"use strict";
var app = angular.module('Demo')
.controller('MainController', ['$rootScope', '$scope', '$location', 'curUser',MainController]);
function MainController($rootScope, $scope, $location, curUser) {
//some logic here
}());
I tried to include a third party module called ngIdle and the resultant code looks like below.
(function () {
"use strict";
var app = angular.module('Demo', ['ngIdle'])
.controller('MainController', ['$rootScope', '$scope', '$location', 'curUser','Idle', function ($rootScope, $scope, $location, curUser, Idle) {
}]).config(function (IdleProvider, KeepaliveProvider) {
// configure Idle settings
IdleProvider.idle(5); // in seconds
IdleProvider.timeout(5); // in seconds
KeepaliveProvider.interval(2); // in seconds
})
.run(function (Idle) {
// start watching when the app runs. also starts the Keepalive service by default.
Idle.watch();
});
}());
Now I get an error as Error: [$injector:unpr] Unknown provider: curUserProvider <- curUser <- MainController. Here's the curUser factory definition
(function () {
"use strict";
angular.module("services").factory("curUser", curUser)
function curUser() {}
}());
curUser is defined in the services module so it needs to be added as a dependency in your app. This would have been the case even before adding ngIdle.
var app = angular.module('Demo', ["ngIdle", "services"])
The factory function should also return something.
function curUser() {
return { username: 'fooBar'};
}
See working plunker.
Related
I am new to angular testing. Facing some issues while testing angular code using jasmine.
It will be highly appreciated if you read my question and try to solve my problem as i googled it but could not find any satisfactory solution
Here is my angular app
var app = angular.module('myApp', ['ngAnimate', 'ui.router', 'ui.bootstrap', 'toggle-switch',
'ngTagsInput', 'blockUI', 'ngBootbox', 'ui.select', 'ngSanitize', 'angular.filter']);
app.config(["$httpProvider", "blockUIConfig", function ($httpProvider, blockUIConfig) {
'use strict';
blockUIConfig.autoBlock = false;
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
$httpProvider.interceptors.push('interceptorService');
}]);
And here is controller file:
app.controller('myController', ['$scope', '$filter', '$http', '$window', '$ngBootbox', '$modal', 'dataservice', 'user', 'message_kinds',
function($scope, $filter, $http, $window, $bB, $modal, dataservice, user, message_kinds) {
$scope.user = user;
/controller logic/
}]);
I want to test this controller if $scope.user equals to user or not.Am using jasmine for testing. Here is spec file.
describe("myController", function() {
beforeEach(module('myApp'));
beforeEach(inject(function(_$controller_){
$controller = _$controller_;
}));
describe("myController testing", function () {
it("should update scope.user", function () {
var user = {customer_id: 1};
var my_controller = $controller('myController', { user: user });
expect(my_controller.user).toEqual(user);
});
});
});
I have also included all dependency files like angular.js, angular-mocks.js etc in SpecRunner.html
Having three problems:
Facing [$injector:unpr] http://errors.angularjs.org/1.4.4/$injector/unpr?p0=interceptorServiceProvide error on app.config block regarding $httpProvider
ReferenceError: $controller is not defined in spec.js at line
var my_controller = $controller('myController', { user: user });
How can I test if scope.user is equals to user in expect block?
1) Check this answer, as regards $http/$httpBackend which might help you - you can adapt this to get the answers you're looking for
2) Have you declared $controller (and now $httpProvider) as a variable in the beginning of the describe() block?
3) You should have that already. Your code, at least as I can see, looks like it should work like you want it to.
I am trying to make a simple login page and click event on login button. I am able to display view. But in between I am getting this error on console:
Error: [ng:areq] http://errors.angularjs.org/1.3.13/ng/areq?p0=controllers%2FLoginCtrl&p1=not%20a%20function%2C%20got%20undefined
at Error (native)
at http://code.ionicframework.com/1.0.0/js/ionic.bundle.min.js:37:417
at Sb (http://code.ionicframework.com/1.0.0/js/ionic.bundle.min.js:50:510)
at tb (http://code.ionicframework.com/1.0.0/js/ionic.bundle.min.js:51:78)
at $get (http://code.ionicframework.com/1.0.0/js/ionic.bundle.min.js:106:331)
at c.controller.I.appendViewElement (http://code.ionicframework.com/1.0.0/js/ionic.bundle.min.js:387:3265)
at Object.c.factory._.create.H.render (http://code.ionicframework.com/1.0.0/js/ionic.bundle.min.js:385:16956)
at Object.c.factory._.create.H.init (http://code.ionicframework.com/1.0.0/js/ionic.bundle.min.js:385:16191)
at c.controller.I.render (http://code.ionicframework.com/1.0.0/js/ionic.bundle.min.js:387:2221)
at c.controller.I.register (http://code.ionicframework.com/1.0.0/js/ionic.bundle.min.js:387:1952)
Here is my code https://dl.dropbox.com/s/mq2vdmxmx5qbfdd/testapp.zip?dl=0
Please run index.html to get error
/*global define, require */
define(function (require) {
'use strict';
var controllers = angular.module('app.controllers', []);
controllers.controller('LoginCtrl', require('controllers/LoginCtrl'));
return controllers;
});
try to "execute" module , note the extra () parenthesis on the end of file
define(function () {
'use strict';
function ctrl($scope, $state) {
$scope.login = function () {
alert("--")
};
}
ctrl.$inject = ['$scope', '$state'];
return ctrl;
}());
I'm using an interceptor to add JWT tokens to my http calls. My code works fine like this:
angular
.module("jwtKickStart")
.factory("authInterceptorService", authInterceptorService);
authInterceptorService.$inject = ["$injector", "$q", "$timeout"];
function authInterceptorService($injector, $q, $timeout) {
var $state, loginModal, $http;
//...
But if I wrap that in an IIFE, then I get an Unknown Provider error:
(function () {
"use strict";
angular
.module("jwtKickStart")
.factory("authInterceptorService", authInterceptorService);
authInterceptorService.$inject = ["$injector", "$q", "$timeout"];
function authInterceptorService($injector, $q, $timeout) {
var $state, loginModal, $http;
//...
Am I not allowed to use the IIFE?
EDIT: here are some more details
The error message is this one: http://errors.angularjs.org/1.3.8/$injector/unpr?p0=authInterceptorServiceProvider%20%3C-%20authInterceptorService%20%3C-%20%24http%20%3C-%20%24templateFactory%20%3C-%20%24view%20%3C-%20%24state
The source code is available here:
https://github.com/capesean/JWTKickStart/tree/master/JWTKickStart.APP/app
try to inject like this :
(function () {
"use strict";
angular
.module("jwtKickStart")
.factory("authInterceptorService",["$injector","$q","$timeout" authInterceptorService]);
function authInterceptorService($injector, $q, $timeout) {
// your code
}
I have ui.bootstrap as a dependency in my app, but I'm having an issue injecting the $modal service into my controller.
I'm getting the following error:
$modal is not defined
in my controller code, specifically in this function below where I attempt to open a modal :
function saveAndDisplayReport() {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});
$location.url('index.html#/?reptname=' + vm.reptName);
}
Here's my reportmaint.js controller code header section, but Im' unclear on how to inject ui.bootstrap (please see the $modal parameter):
(function () {
'use strict';
var controllerId = 'reportmaint';
angular.module('app').controller(controllerId, ['$rootScope', '$scope', '$location', 'common', 'datacontext',
'gridHierarchyService', 'reportsContext', '$modal', reportmaint]);
function reportmaint($rootScope, $scope, $location, common, datacontext, gridHierarchyService, reportsContext) {
var getLogFn = common.logger.getLogFn;
var log = getLogFn(controllerId);
var logErr = getLogFn("error");
...
})();
and here is my app.js where 'ui.bootstrap' is defined:
(function () {
'use strict';
var app = angular.module('app', [
// Angular modules
'ngAnimate', // animations
'ngRoute', // routing
'ngSanitize', // sanitizes html bindings (ex: sidebar.js)
// Custom modules
'common', // common functions, logger, spinner
'common.bootstrap', // bootstrap dialog wrapper functions
// 3rd Party Modules
'ui.bootstrap', // ui-bootstrap (ex: carousel, pagination, dialog)
'kendo.directives', // Kendo UI
'app.customcontrollers' // Language/Currency settings
//'ngjqxsettings' // jQWidgets init and directives (loaded in index.html)
]);
app.run(['$route', '$rootScope', 'common', 'userService', function ($route, $rootScope, common, userService) {
console.log("In app.run");
var getLogFn = common.logger.getLogFn;
var log = getLogFn('app');
}]);
})();
and in my index.html file I have the script reference :
<script src="scripts/ui-bootstrap-tpls-0.10.0.js"></script>
I am using this plunker as a live example, but I'm still going wrong somewhere - http://plnkr.co/edit/KsADLPaOfY7rtPTdWyYn?p=preview
thanks in advance for your help...
Bob
it seems like you are not injecting the modal at all ($modal is missing) in the function of your controller; try something like:
I'm not sure if reportmaint is a service, if not, just remove it
angular.module('app').controller('reportmaint', ['$rootScope', '$scope', '$location', 'common', 'datacontext','gridHierarchyService', 'reportsContext', '$modal', 'reportmaint',
function($rootScope, $scope, $location, common, datacontext, gridHierarchyService, reportsContext, $modal, reportmaint) {
//Client code
}
]);
I have 2 Angular modules. rootModule:
var myModule = angular.module('rootModule', []);
myModule.factory('mySharedService', function($rootScope) {
var sharedService = {};
return sharedService;
});
myModule.factory('getMonthlyHistoryService', function ($http, $q) {
var getMonthlyHistoryService = {};
return getMonthlyHistoryService;
});
function RandomScaleController($scope, $rootScope, sharedService) {
}
RandomScaleController.$inject = ['$scope', '$rootScope', 'mySharedService'];
and Child module:
var modal = angular.module('modal', ['rootModule', 'ui.bootstrap']);
function MomController($scope, $http, sharedService, getMonthlyHistoryService) {
}
MomController.$inject = ['$scope', '$http', 'mySharedService','getMonthlyHistoryService'];
All works fine, but if i move getMonthlyHistoryService into child module i got Error: Unknown provider: getMonthlyHistoryServiceProvider <- getMonthlyHistoryService.
How I can move getMonthlyHistoryService into child module?
How module reference should be created would be to have multiple child modules linked\imported into the parent module
You have done the reverse. You have injected parent module into child module. Now if you move your service from parent to child only this module element can access this service.
Try
var myModule = angular.module('rootModule', ['modal']);
and
var modal = angular.module('modal', ['ui.bootstrap']);
Also declare the controller using the module.controller syntax
modal.controller('MomController',['$scope', '$http', 'mySharedService','getMonthlyHistoryService',function($scope, $http, sharedService, getMonthlyHistoryService) {
}]);