Error: [$injector:unpr] Unknown provider - while migrating AngularJS1 to Angular6 - angularjs

I'm trying to do upgrade components written in AngularJS1 to Angular6. I'm taking the approach of having the wrappers for all the existing AngularJS1 component by extending "UpgradeComponent" placed under the folder "directive-wrappers" in my example.
while loading the application I get the console error as
Error: [$injector:unpr] Unknown provider: testDirective2DirectiveProvider <- testDirective2Directive
https://errors.angularjs.org/1.7.8/$injector/unpr?p0=testDirective2DirectiveProvider%20%3C-%20testDirective2Directive
at eval (angular.js:138)
at eval (angular.js:4924)
at Object.getService [as get] (angular.js:5084)
at eval (angular.js:4929)
at Object.getService [as get] (angular.js:5084)
at Function.UpgradeHelper.getDirective (upgrade_helper.ts:56)
at new UpgradeHelper (upgrade_helper.ts:52)
at TestDirective2Wrapper.UpgradeComponent (upgrade_component.ts:106)
at new TestDirective2Wrapper (TestDirective2Wrapper.ts:27)
at createClass (provider.ts:265) "<app-root _nghost-c0="">"
To mimic the same application online I have created the same angularjs directives and its wrappers in stackblitz

There's an issue with the docsSampleDirective.js file.
You've initialized the AngularJS Module there for the second time when you already initialized the testApp module in the app.module.js file. Just get rid of the dependency array from there:
angular
.module("testApp")
.controller("Controller", [
"$scope",
function($scope) {
$scope.customer = {
name: "Naomi",
address: "1600 Amphitheatre"
};
}
])
.directive("docsSimpleDirective", function() {
return {
template: "Name: {{customer.name}} Address: {{customer.address}}"
};
});
Here's a Working Sample StackBlitz for your ref.

Related

Not able to load module in AngularJs, after deploying to server(GCP-AppEngine). Error: [$injector:unpr]

I have a piece of code(abc.ts file), which is responsible for loading a HTML related content using $routeProvider and angular.module.
Respective file is loading properly in local machine, but in server its resulting in a error as stated below.
Note: we are using AngularJs with TypeScript and JavaScript combination, also using Closure. Eventually JS & closure usage is being removed.
abc.ts file:
import 'angular-route';
import * as angular from 'angular';
import {AbcLinkController} from './filepath';
/** #ngInject */
function configureRoutes($routeProvider: angular.route.IRouteProvider) {
$routeProvider.when('abcLink/:pathVariable1:pathVariable2', {
templateUrl:'abclink/abc-file-name.ng',
controller: AbcLinkController,
controllerAs: 'ctrl'
});
}
export const module: angular.IModule =
angular.module('abc', ['ngMaterial', 'ngRoute'])
.config(configureRoutes);
Sinppet from app.js file:
module =
angular
.module(
'applicationName.app',
[
'ngMockE2E',
'ngRoute',
'ngSanitize',
'angularMoment',
abc.module.name,
...
Browser console error:
Error: [$injector:unpr] http://errors.angularjs.org/1.6.4-local+sha.617b36117/$injector/unpr?p0=%24%24routeParams%24%24Provider%20%3C-%20%24%24routeParams%24%24
at js.js?437e15ca-1865-4083-a6cd-f574d2d2b01d:4107
at js.js?437e15ca-1865-4083-a6cd-f574d2d2b01d:4142
at Object.y [as get] (js.js?437e15ca-1865-4083-a6cd-f574d2d2b01d:4139)
at js.js?437e15ca-1865-4083-a6cd-f574d2d2b01d:4142
at y (js.js?437e15ca-1865-4083-a6cd-f574d2d2b01d:4139)
at v (js.js?437e15ca-1865-4083-a6cd-f574d2d2b01d:4140)
at Object.instantiate (js.js?437e15ca-1865-4083-a6cd-f574d2d2b01d:4140)
at js.js?437e15ca-1865-4083-a6cd-f574d2d2b01d:4192
at Object.link (js.js?437e15ca-1865-4083-a6cd-f574d2d2b01d:4509)
at js.js?437e15ca-1865-4083-a6cd-f574d2d2b01d:4117 "<div ng-view=\"\" class=\"ng-scope\" data-ng-animate=\"1\">"
What could be wrong here?

Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource <- EventService?

I'm testing an ionic app created using creator.
I put the following code in services.js.
angular.module('app.services', [])
.factory('BlankFactory', [function(){
}])
.factory('EventService', ['$resource', function ($resource) {
return $resource('https://.....net/api/events/:id');
}])
.service('BlankService', [function(){
}]);
And the following code in controllers.js.
angular.module('app.controllers', [])
......
.controller('nightlifeCtrl', function ($scope, EventService) {
$scope.events = EventsService.query({category: "Nightlife"});
})
Hoever I got the following error in js console?
ionic.bundle.js:25642 Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource <- EventService
http://errors.angularjs.org/1.4.3/$injector/unpr?p0=%24resourceProvider%20%3C-%20%24resource%20%3C-%20EventService
at ionic.bundle.js:13380
at ionic.bundle.js:17574
at Object.getService [as get] (ionic.bundle.js:17721)
at ionic.bundle.js:17579
at getService (ionic.bundle.js:17721)
at Object.invoke (ionic.bundle.js:17753)
at Object.enforcedReturnValue [as $get] (ionic.bundle.js:17615)
at Object.invoke (ionic.bundle.js:17762)
at ionic.bundle.js:17580
at getService (ionic.bundle.js:17721)
I tried to add the dependency by angular.module('...', ['ngResource']) but it got the following error.
ionic.bundle.js:17697 Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module app.controllers due to:
Error: [$injector:modulerr] Failed to instantiate module ngResource due to:
And I didn't find the file angular-resource.js in the js files. Should I avoid ngResource and use plain ajax call?
In index.html
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/routes.js"></script>
<script src="js/services.js"></script>
<script src="js/directives.js"></script>
Its not working because you have not loaded the angular-resource dependency. If you want to use an external module in angular, you need to load it as a dependency in your module definition by passing name of the module in the second array parameter to angular.module. In your case name of the module is ngResource. Load the script file for angular-resource.js and then add it as dependency as below:
angular.module('app.services', ['ngResource'])
Also I see that you have not loaded app.services as dependency in app.controller. You need to either load that dependency or load both these dependencies in your app definition module as:
angular.module('app', [
'app.services',
'app.controllers'
]);
Updated JSBIN with fix for console error
Your code also had one typo. You were referring to EventsService in your controller while the name was EventService. I have fixed that as well in the jsbin

AngularJS - Error: [$injector:unpr] Unknown provider:

I wanted to rewrite my existing code, but I failed as I run into this error:
ionic.bundle.js:8900 Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module app.routes due to:
Error: [$injector:unpr] Unknown provider:
My working snippet:
angular.module('app.routes', [])
.config(function() {});
My rewritten snippet (this one fails):
(function () {
// use strict mode to write clean code!
'use strict';
// This configures the routes
var RouteProvider = function () {
};
// init the config
angular.module('app.routes', [])
.config(['', RouteProvider]);
}());
I have created a Plunker for this: Plunker
Thanks for your help ;)
You have an empty string into your .config call - this is effectively telling Angular you want to inject a service with a blank string as the name into your config function. This doesn't exist, so you get an unknown provider error - a pretty unclear one at that, because it tries to show you the name of the service, but it's blank, so you just end up with Error: [$injector:unpr] Unknown provider: and nothing else!
Your .config call should look more like this:
// init the config
angular.module('app.routes', [])
.config(RouteProvider);
You don't need the array syntax if you're not actually injecting anything into the function.

Error: [$injector:unpr] Unknown provider: chart.jsProvider

I am trying to use this plugin https://github.com/jtblin/angular-chart.js.
And I am getting this error (I don't think the problem is with the plugin instead it's in the way I'm doing the injection!!):
Error: [$injector:unpr] Unknown provider: chart.jsProvider <- chart.js <- WhateverCtrl
http://errors.angularjs.org/1.3.20/$injector/unpr?p0=chart.jsProvider%20%3C-hart.js%20%3C-%20WhateverCtrl
at http://mega.app/scripts/vendor.js:9895:12
at http://mega.app/scripts/vendor.js:13863:19
at Object.getService [as get] (http://mega.app/scripts/vendor.js:14010:39)
at http://mega.app/scripts/vendor.js:13868:45
at getService (http://mega.app/scripts/vendor.js:14010:39)
at invoke (http://mega.app/scripts/vendor.js:14042:13)
at Object.instantiate (http://mega.app/scripts/vendor.js:14059:27)
at http://mega.app/scripts/vendor.js:18356:28
at http://mega.app/scripts/vendor.js:44696:28
at invokeLinkFn (http://mega.app/scripts/vendor.js:18113:9)
when I inject chart.js globally like below, I don't get any error. (keep reading)
angular.module('my-app', [
'chart.js' // <<<<<
'ui.router',
'ngStorage',
// ...
]);
})();
But from my understanding, it's recommended to inject this module only in the controllers that uses it, thus when I try to inject it in a controller like below, I get the error above.
angular
.module('my-app')
.controller('WhateverCtrl', ctrl);
ctrl.$inject = ['chart.js']; // <<<<<
function ctrl() {
var vm = this;
// ...
However if I remove the $ from the injection line to ctrl.inject = ['chart.js']; I do get rid of the error, but chart.js wont work, because I guess I must pass it to the function function ctrl() { like this function ctrl(chart.js) { which of course causes an error due to the ..
Since angular-chart.js is itself a module, it must be injected into the module and cannot be injected into the controller.
With JGOakley's clarification, I was able to discover this line in angular-chart.js
return angular.module('chart.js', [])
.provider('ChartJs', ChartJsProvider)
To include this for use in your controller:
YourModule.$inject = ['ChartJs'];
This was a frustrating find, since I took this line to mean I could reference it as chart
define(['angular', 'chart'], factory);

add sweet alert angular js

I'm new to AngularJS and I'm trying to use sweet alert plugin from https://github.com/oitozero/ngSweetAlert , I already added the corresponding scripts to my index.html like this :
index.html
<link rel="stylesheet" href="bower_components/sweetalert/dist/sweetalert.css">
<script src="bower_components/angular-sweetalert/SweetAlert.min.js"></script>
<script src="bower_components/sweetalert/dist/sweetalert.min.js"></script>
As the documentacion says. Now in my ctrl.js I have this :
var ctrl = function ($scope, SweetAlert) {
SweetAlert.swal("Here's a message"); // this does not work
}
ctrl.$inject = ['$scope', 'oitozero.ngSweetAlert];
angular.module('myApp.missolicitudes.controllers',
[
'oitozero.ngSweetAlert'
])
.controller('MiSolicitudCtrl', ctrl);
But is not working, on my browser I got this error from developer tools:
Error: [$injector:unpr]
http://errors.angularjs.org/1.4.3/$injector/unpr?p0=oitozero.ngSweetAlertProvider%20%3C-""itozero.ngSweetAlert%20%3C-%20MiSolicitudCtrl
at Error (native)
at http://localhost:8081/assets/libs/angular/angular.min.js:6:416
at http://localhost:8081/assets/libs/angular/angular.min.js:40:375
at Object.d [as get] (http://localhost:8081/assets/libs/angular/angular.min.js:38:364)
at http://localhost:8081/assets/libs/angular/angular.min.js:40:449
at d (http://localhost:8081/assets/libs/angular/angular.min.js:38:364)
at e (http://localhost:8081/assets/libs/angular/angular.min.js:39:124)
at Object.instantiate (http://localhost:8081/assets/libs/angular/angular.min.js:39:273)
at $get (http://localhost:8081/assets/libs/angular/angular.min.js:80:228)
at link (http://localhost:8081/assets/libs/angular/angular-route.min.js:7:268)
How can I implement this plugin correctly?
Update 1
I have already try this suggestion by #Pankaj Parkar and #Mike G
ctrl.$inject = ['$scope', 'oitozero.ngSweetAlert'];
and like this
ctrl.$inject = ['$scope', 'SweetAlert'];
My developer tools throws this message:
Error: [$injector:unpr] http://errors.angularjs.org/1.4.3/$injector/unpr?p0=oitozero.ngSweetAlertProvider%20%3C-"<div class="container ng-scope" ng-view="">"itozero.ngSweetAlert%20%3C-%20MiSolicitudCtrl
at Error (native)
at http://localhost:8081/assets/libs/angular/angular.min.js:6:416
at http://localhost:8081/assets/libs/angular/angular.min.js:40:375
at Object.d [as get] (http://localhost:8081/assets/libs/angular/angular.min.js:38:364)
at http://localhost:8081/assets/libs/angular/angular.min.js:40:449
at d (http://localhost:8081/assets/libs/angular/angular.min.js:38:364)
at e (http://localhost:8081/assets/libs/angular/angular.min.js:39:124)
at Object.instantiate (http://localhost:8081/assets/libs/angular/angular.min.js:39:273)
at $get (http://localhost:8081/assets/libs/angular/angular.min.js:80:228)
at link (http://localhost:8081/assets/libs/angular/angular-route.min.js:7:268)
Here is a simple module that I wrote to make SweetAlert work.
// sweetalert.js
angular
.module('sweetalert', [])
.factory('swal', SweetAlert);
function SweetAlert() {
return window.swal;
};
Hence, no need to use any other library to use sweetalert, simply write your own.
Simply inject the module in the controller where you want to use it.
Example
angular
.module('demo', ['sweetalert'])
.controller('DemoController', DemoController);
function DemoController($scope) {
$scope.btnClickHandler = function() {
swal('Hello, World!');
};
};
Here is an example gist in coffeescript: https://gist.github.com/pranav7/d075f7cd8263159cf36a
I got it work, by NOT injecting it in the module.
my ctrl.js just got like this
ctrl.$inject = ['$scope'];
and inside my controller y just call it like this
var ctrl = function ($scope) {
swal("Here's a message");
}
And it works!, i dont know if the correct way... but at least works.
Inject sweetalert.min.js and sweetalert.css.
Used like this code in your controller
swal({
type: "error",
title: "Error!",
text: "fail",
confirmButtonText: "OK"
});
`
Never inject a module into to controller as dependency. You should inject SweetAlert factory there which has various function to show alerts. Also add the missing ' qoute on the factory injection.
You should use
ctrl.$inject = ['$scope', 'SweetAlert'];//<==`oitozero.ngSweetAlert` module
//could be injectable inside your app module.
Demo Plunkr

Resources