Error : Unknown provider After JavaScript Compression - javascript

I have below AngularJS controller code
(function() {
'use strict';
angular
.module('app')
.controller('TemplateCtrl', TemplateCtrl);
function TemplateCtrl($http, $auth, $rootScope,$scope){
}
})();
After compression from http://jscompress.com/ I got below output.
!function(){"use strict";function t(t,l,n,e){}angular.module("app").controller("TemplateCtrl",t)}();
Before compression there was no error but after compression I am getting below error
Error: [$injector:unpr] Unknown provider: tProvider <- t <- TemplateCtrl
I am not finding any clue to fix this ?
Thanks for your help and time.

For angular compressing you need to do some extra stuff. You need to let it know how to compress dependencies. So you need this:
(function() {
'use strict';
angular
.module('app')
.controller('TemplateCtrl', ["$http", "$auth", "$rootscope", "$scope", TemplateCtrl]);
function TemplateCtrl($http, $auth, $rootScope,$scope){
}
})();

Angular resolves the dependency based on the names.
Read more here: https://stackoverflow.com/a/35336414/2405040 (Dependency Annotation)
And change your code like this:
(function() {
'use strict';
angular
.module('app')
.controller('TemplateCtrl', ["$http", "$auth", "$rootscope", "$scope", TemplateCtrl]);
function TemplateCtrl($http, $auth, $rootScope,$scope){
}
})();
To prevent missing things for minification and write code using inline annotation syntax, add the ng-strict-di annotation with ng-app attribute.

Below code worked for me.
(function() {
'use strict';
angular
.module('app')
.controller('TemplateCtrl', TemplateCtrl);
TemplateCtrl.$inject = ['$http', '$auth', '$rootScope', '$scope'];
function TemplateCtrl($http, $auth, $rootScope,$scope){
}
})();
After compression this is
!function(){"use strict";function t(t,o,e,c){}angular.module("app").controller("TemplateCtrl",t),t.$inject=["$http","$auth","$rootScope","$scope"]}();
Thanks to all.

wright you controller in this way :
angular
.module('app')
.controller('TemplateCtrl', function () {
var something = function ($http, $auth, $rootScope,$scope){
}
});

Related

Injector error in Angular JS with Jhipster

I have an problem to import "oi.select" in my jhipster project.
My controller js file :
(function() {
'use strict';
angular
.module('myApp')
.controller('UserProfileDialogController', UserProfileDialogController);
UserProfileDialogController.$inject = ['$timeout', '$scope', '$stateParams', '$uibModalInstance', 'entity', 'UserProfile', 'MdmEnumeration','oi.select'];
function UserProfileDialogController ($timeout, $scope, $stateParams, $uibModalInstance, entity, UserProfile, MdmEnumeration,oiSelect) {
} ....
I have already inject in my index.html :
<script src="bower_components/oi.select/dist/select-tpls.min.js"></script>
<link rel="stylesheet" href="bower_components/oi.select/dist/select.min.css">
I get the followin error :
angular.js:13550 Error: [$injector:unpr] Unknown provider: oi.selectProvider <- oi.select <- UserProfileDialogController
Any ideas ?
i solved this. I have a file : app.module.js which contains all modules to inject in the app. I add "oi.select" and it works. Thanks for your help

$routeProvider config route throwing 'Uncaught Error: [ng:areq] Argument 'fn' is not a function, got string'

I'm writing routing logic using ngRoute of angular JS. The following is my code.
index.js
(function() {
'use strict';
function config($routeProvider, $httpProvider, cfpLoadingBarProvider, $tooltipProvider) {
$routeProvider.otherwise({redirectTo: '/404'});
$httpProvider.defaults.withCredentials = false;
$httpProvider.defaults.headers.common['content-type'] = "application/json";
}
angular
.module('pacman', ['ngCookies', 'ngRoute', 'ui.bootstrap', 'ui.validate',
'angular-cache', 'angular-loading-bar', 'angular-md5', 'rt.iso8601', 'ngAnimate']
)
.config(['$routeProvider', '$httpProvider', 'cfpLoadingBarProvider', '$tooltipProvider', config])
.run(['$rootScope', '$location', '$modalStack', '$cookies']);
})();
app.controller.js
(function() {
'use strict';
function config($routeProvider) {
$routeProvider.when('/', {
templateUrl: 'app/components/landingpage/landingpage.html',
controller: 'appController'
});
}
function appController($scope, $rootScope, $location) {
$scope.submitLogin = function() {
alert("Successfully loggedIn");
};
}
angular
.module('pacman')
.controller('appController', ['$scope', '$rootScope', '$location', appController])
.config(['$routeProvider', config]);
})();
notFound.controller.js
(function() {
'use strict';
function config($routeProvider) {
$routeProvider.when('/404', {
templateUrl: 'app/components/notFound/404page.html',
controller: 'notFoundController'
});
}
function notFoundController($scope, $rootScope, $location) {
debugger;
}
angular
.module('pacman')
.controller('notFoundController', ['$scope', '$rootScope', '$location', notFoundController])
.config(['$routeProvider', config]);
})();
My code is a simple app. I'm trying to load different controllers based on routes. However at the time of loading the app, in the last controller's '$routeProvider' it throws an error
Uncaught Error: [ng:areq] Argument 'fn' is not a function, got string
http://errors.angularjs.org/1.4.8/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20string
I have no clue how to figure out the problem. Any leads would be appreciated.
The following is my library bundle order.
'node_modules/jquery/dist/jquery.js',
'node_modules/angular/angular.js',
'node_modules/angular-route/angular-route.js',
'node_modules/jquery.transit/jquery.transit.js',
'node_modules/angular-cache/dist/angular-cache.js',
'node_modules/angular-cookies/angular-cookies.js',
'node_modules/angular-loading-bar/build/loading-bar.js',
'node_modules/angular-ui-validate/dist/validate.js',
'node_modules/chart.js/Chart.js',
'node_modules/angular-md5/angular-md5.js',
'node_modules/angular-iso8601/dist/angular-iso8601.js',
'node_modules/angular-animate/angular-animate.js',
'node_modules/angular-chart.js/dist/angular-chart.js',
'node_modules/rx/dist/rx.all.js',
'node_modules/angular-ui-bootstrap/ui-bootstrap-tpls.js',
'node_modules/bootstrap/dist/js/bootstrap.js'
Kindly help.
Issue is in your index.js where you define the run method on your angular app.
angular
.module('pacman', []) // removed dependencies for brevity
.run(['$rootScope', '$location', '$modalStack', '$cookies']);
The last argument in the array passed to run should be a function but you forgot to pass a function. Change your run to add some implementation like below or remove the run if you don't see any use for it.
angular.module('pacman', []) // removed dependencies for brevity
.run(['$rootScope', '$location', '$modalStack', '$cookies',
function($rootScope,$location,$modalStack,$cookies){
// some statements here
}]);
Angular JS file declaration must come before the jquery in your index.html
'node_modules/angular/angular.js',
'node_modules/jquery/dist/jquery.js',

What's wrong with this factory dependencies issue?

I'm working with AngularJS.
I'd like to get a controller using a first factory which using another one.
It could be schematize like that:
MyCtrl -> Factory1 -> Factory2
So I tried to do in 3 different files (loaded in the following order):
Factory2.js
app.factory('Factory2', function () { ... })
Factory1.js
app.factory('Factory1',['Factory2', function (Factory2) { ... })
controller.js
app.controller('MyCtrl',['$scope', 'Factory1', function ($scope, Factory1) { ... })
And in my HTML I have:
<script src="services/factory2.js" type="text/javascript"></script>
<script src="services/factory1.js" type="text/javascript"></script>
<script src="controllers/controller.js" type="text/javascript"></script>
But it doesn't work and I've got this error Unknown provider: Factory2Provider <- Factory2 <- Factory1
What's wrong with my code? Am I missing something?
You can refactor your codes and use modules, in this way you will not need to use $inject
var app = angular.module('app', ['factories', 'mymodule']);
angular.module('factories', [])
.factory('Factory2', function () { })
.factory('Factory1', ['Factory2', function (Factory2) {
return myCustomFunction = function () {
alert('todo');
}
}]);
angular.module('mymodule', [])
.controller('MyCtrl', ['$scope', 'Factory1', function ($scope, Factory1) {
$scope.text = "testing";
}])
http://jsfiddle.net/kL78rdr3/3/
Why don't you use explicit injection with $inject? It is a better approach, because it gives you more control over the dependencies. For example:
userController.js
function userController (model, routeParams, searchService) {
//implementation
}
userController.$inject = ['$scope', '$routeParams', 'searchService'];
app.controller("userController", userController);
searchService.js
var searchService = function (http, log) {
//implementation
}
searchService.$inject = ["$http", "$log"];
app.factory("searchService", searchService);
This post may be useful: Explicit Dependency Injection

Error: Unknown provider - Karma, requirejs, angular

I get this error when i try to make a test
Error: [$injector:unpr] Unknown provider: $translateProvider <- $translate
I'm using karma with requirejs.
loadingCtrlSpec.js
define([
'angular',
'angular-mocks',
'app',
'angular-translate'
], function(angular, mocks, app) {
'use strict';
describe('loadingCtrl', function(){
var ctrl, scope, translate;
beforeEach(mocks.module('TestApp'));
beforeEach(inject(function($injector){
scope = $injector.get('$rootScope').$new();
translate = $injector.get('$translate');
}));
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});
});
loadingCtrl.js
define(['angular'], function (angular) {
'use strict';
angular.module('TestApp', [])
.controller('loadingCtrl', ['$scope', '$translate', function($scope, $translate) {
$translate(['build.DEFAULT_EMAIL_SUBJECT','build.DEFAULT_EMAIL_NOTES']).then(function (translations) {
$scope.title = translations["build.DEFAULT_EMAIL_SUBJECT"];
$scope.notes = translations["build.DEFAULT_EMAIL_NOTES"];
});
}]); })
If i don't use angular-translate ($translate) everything is working so i don't think the problem is from karma.conf.js or test-main.js (require.conf for karma).
Your TestApp module will need to specify the pascalprecht.translate module as a dependency. Also be sure to include angular-translate as a dependency when defining your main module so the relevant script gets loaded:
define(['angular', 'angular-translate'], function (angular) {
angular.module('TestApp', ['pascalprecht.translate']);
});

RequireJS and AngularJS multiple controllers

I'm using latest version of Require and Angular, but i encountered strange bug. I have controller for every view, but apparently works only for one view. This is my example code:
Define all controllers: Controllers.js
define([
'modules/index/controller',
'modules/test/controller'
], function(){});
Here works only with one controller, if i include 2 like here i get Error: ng:areq
Bad Argument
Index: controller.js
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
Test: controller.js
define(['angular'], function(angular){
'use strict';
return angular.module('myApp.controllers', [])
.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
Where i'm wrong?
The problem is that you are creating the myApp.controllers module twice and one is overwriting the other. Assuming that you are manually bootstrapping angular after loading the controller, what you should be doing is to create the module first, then load that module to create controller:
app.js
define(['angular'], function(){
'use strict';
return angular.module('myApp.controllers', []);
});
Index: controller.js
define(['app'], function(app){
'use strict';
return app.controller('indexCtrl', ['$scope' , function ($scope){
alert("index ok");
}]);
});
Test: controller.js
define(['app'], function(app){
'use strict';
return app.controller('testCtrl', ['$scope' , function ($scope){
alert("test ok");
}])
});
I created angularAMD to facilitate use of RequreJS and AngularJS that might be of interest to you:
http://marcoslin.github.io/angularAMD/

Categories