Constant accessible from all modules - angularjs

I'd like to have constant that is accessible from my other modules.
I tried something like below, but there's exception.
How should I modify my code to make it work?
angular.module('starter', ['starter.Authentication'])
.constant('baseUrl', 'http://myUrl/api/');
angular.module('starter.Authentication', ['baseUrl'])
.factory('AuthenticationService',
['baseUrl',
function (baseUrl) {
var service = {};
return service;
};
Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to:
Error: [$injector:modulerr] Failed to instantiate module starter.Authentication due to:
Error: [$injector:modulerr] Failed to instantiate module baseUrl due to:
Error: [$injector:nomod] Module 'baseUrl' 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.

Change:
angular.module('starter.Authentication', ['baseUrl'])
To:
angular.module('starter.Authentication', ['starter'])
This is because starter is a module that holds the baseUrl constant.

Related

AngularJS component won't instantiate, "'module' is not a function"

I've been asked to do a project in Angular 1, and I've been having a nightmare of a time just trying to get the page to load the app code from bundle.js. I've currently got an error along the lines of
[$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module {"_invokeQueue":
[],"_configBlocks":[],"_runBlocks":[],"name":"approveComponent"} due to:
Error: [ng:areq] Argument 'module' is not a function, got Object
but the only approveComponent I've got is essentially a blank placeholder -
import angular from 'angular';
const approve = angular.module('approveComponent', []);
export default approve;
What can I do to get this actually running?
I'm using Webpack 4 to bundle everything. (NO errors from webpack.)
I have seen this error earlier.You need to pass module name not module in the dependency
ngModule = angular.module('webpack', [
require('./webpack/photocrop/client')//this is a object
])
You can simply require the file and just inject by name Like this
var firstModule = require('./webpack/myapp/client')
ngModule = angular.module('webpack', [
'firstModule'//this is should work
])
firstModule(ngModule)
SAMPLE GITHUB REPO

angular_1.default.module Uncaught TypeError: Cannot read property 'module' of undefined

I am creating a starter template using angular 1.6, webpack, gulp and typescript. I am able to compile and build my application but app is not bootstrapping. The issue is weird as when I am debugging angular.module is defined and module can be created from console angular.module('testModule', []) but as resume debugger I get exception in core.module.ts at export const CoreModule = angular.module('app.core', []).name;.
I have further investigated this issue by disabling source code and I have found the problem. The above mentioned code compiles to exports.CoreModule = angular_1.default.module('app.core', []).name; where there is no property named default on angular_1.
For code refer https://github.com/muditsaurabh/awesome-ng-starter
Uncaught TypeError: Cannot read property 'module' of undefined
at Object.<anonymous> (app.bundle.js?cd4d443…:1)
at e (vendor.bundle.js?cd4d443…:1)
at Object.<anonymous> (app.bundle.js?cd4d443…:1)
at e (vendor.bundle.js?cd4d443…:1)
at Object.<anonymous> (app.bundle.js?cd4d443…:1)
at e (vendor.bundle.js?cd4d443…:1)
at window.webpackJsonp (vendor.bundle.js?cd4d443…:1)
at app.bundle.js?cd4d443…:1
vendor.bundle.js?cd4d443…:6Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:nomod] Module 'app' 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.6.1/$injector/nomod?p0=app
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:377
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11230
at n (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:10668)
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11004
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20796
at r (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:809)
at h (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20644)
at le (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:22631)
at a (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8560)
at st (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8870)
http://errors.angularjs.org/1.6.1/$injector/modulerr?p0=app&p1=Error%3A%20%…2F%2Flocalhost%3A8000%2Fvendor.bundle.js%3Fcd4d4432605c6f5be56a%3A6%3A8870)
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:377
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11230
at n (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:10668)
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11004
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20796
at r (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:809)
at h (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20644)
at le (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:22631)
at a (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8560)
at st (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8870)
http://errors.angularjs.org/1.6.1/$injector/modulerr?p0=app&p1=Error%3A%20%…2F%2Flocalhost%3A8000%2Fvendor.bundle.js%3Fcd4d4432605c6f5be56a%3A6%3A8870)
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:377
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:21073
at r (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:809)
at h (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20644)
at le (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:22631)
at a (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8560)
at st (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8870)
at at (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8109)
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:11:13916
at HTMLDocument.n (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:17809)
I have replaced import angular from 'angular' with import * as angular from 'angular' and now it is working fine.
Are you sure that AngularJS gets included before your script? The error means that the object angular hasn't been seen by Javascript yet and therefore module cannot be called on it.
You can also just add
"esModuleInterop": true,
to your tsconfig.json instead of using a star import everywhere.

I got error when I use ngPrint

I develop gulp application. there are I use ngPrint for print table.
This is module include,
var myapp = angular.module('myapp', ['ngPrint']);
and html code like this,
<h1 id="printThisElement">Hello Bro!</h1>
<button class="btn btn-primary" ng-print print-element-id="printThisElement">
I use for that development this example.
below my error,
Uncaught Error: [$injector:modulerr] Failed to instantiate module lgps due to:
Error: [$injector:modulerr] Failed to instantiate module ngPrint due to:
Error: [$injector:nomod] Module 'ngPrint' 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.8/$injector/nomod?p0=ngPrint
at http://localhost:8000/vendor/angular.js:68:12
at http://localhost:8000/vendor/angular.js:2005:17
at ensure (http://localhost:8000/vendor/angular.js:1929:38)
at module (http://localhost:8000/vendor/angular.js:2003:14)
at http://localhost:8000/vendor/angular.js:4435:22
at forEach (http://localhost:8000/vendor/angular.js:340:20)
at loadModules (http://localhost:8000/vendor/angular.js:4419:5)
at http://localhost:8000/vendor/angular.js:4436:40
at forEach (http://localhost:8000/vendor/angular.js:340:20)
at loadModules (http://localhost:8000/vendor/angular.js:4419:5)
http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=ngPrint&p1=Error%3A…dModules%20(http%3A%2F%2Flocalhost%3A8000%2Fvendor%2Fangular.js%3A4419%3A5)
at http://localhost:8000/vendor/angular.js:68:12
at http://localhost:8000/vendor/angular.js:4458:15
at forEach (http://localhost:8000/vendor/angular.js:340:20)
at loadModules (http://localhost:8000/vendor/angular.js:4419:5)
at http://localhost:8000/vendor/angular.js:4436:40
at forEach (http://localhost:8000/vendor/angular.js:340:20)
at loadModules (http://localhost:8000/vendor/angular.js:4419:5)
at createInjector (http://localhost:8000/vendor/angular.js:4344:11)
at doBootstrap (http://localhost:8000/vendor/angular.js:1676:20)
at bootstrap (http://localhost:8000/vendor/angular.js:1697:12)
http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=lgps&p1=Error%3A%20…otstrap%20(http%3A%2F%2Flocalhost%3A8000%2Fvendor%2Fangular.js%3A1697%3A12)
How I solve that question I try lots way but I can't get result.
The error suggests angular cant find ngPrint module. Did you include the required js files ?
<link rel="stylesheet" href="https://cdn.rawgit.com/gilf/ngPrint/master/ngPrint.min.css" />
<script src="https://rawgit.com/gilf/ngPrint/master/ngPrint.min.js"></script>
Cheers

how to remove given error " Failed to instantiate module app due to:"

I am trying to make simple app in angular js .I includes js files in my project. But I am getting this error
**Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module app.stationselect due to:
TypeError: $urlRouterProvider.state is not a function
at routeChange (http://localhost:63342/www/js/stationselect/router.js:9:28)
at Object.invoke (http://localhost:63342/www/lib/ionic.bundle.js:12877:17)
at runInvokeQueue (http://localhost:63342/www/lib/ionic.bundle.js:12783:35)
at http://localhost:63342/www/lib/ionic.bundle.js:12792:11
at forEach (http://localhost:63342/www/lib/ionic.bundle.js:9015:20)
at loadModules (http://localhost:63342/www/lib/ionic.bundle.js:12773:5)
at http://localhost:63342/www/lib/ionic.bundle.js:12790:40
at forEach (http://localhost:63342/www/lib/ionic.bundle.js:9015:20)
at loadModules (http://localhost:63342/www/lib/ionic.bundle.js:12773:5)
at createInjector (http://localhost:63342/www/lib/ionic.bundle.js:12699:11)
http://errors.angularjs.org/1.3.13/$injector/modulerr?p0=app.stationselect&…(http%3A%2F%2Flocalhost%3A63342%2Fwww%2Flib%2Fionic.bundle.js%3A12699%3A11)
at REGEX_STRING_REGEXP (http://localhost:63342/www/lib/ionic.bundle.js:8755:12)
at http://localhost:63342/www/lib/ionic.bundle.js:12812:15
at forEach (http://localhost:63342/www/lib/ionic.bundle.js:9015:20)
at loadModules (http://localhost:63342/www/lib/ionic.bundle.js:12773:5)
at http://localhost:63342/www/lib/ionic.bundle.js:12790:40
at forEach (http://localhost:63342/www/lib/ionic.bundle.js:9015:20)
at loadModules (http://localhost:63342/www/lib/ionic.bundle.js:12773:5)
at createInjector (http://localhost:63342/www/lib/ionic.bundle.js:12699:11)
at doBootstrap (http://localhost:63342/www/lib/ionic.bundle.js:10137:20)
at bootstrap (http://localhost:63342/www/lib/ionic.bundle.js:10158:12)
http://errors.angularjs.org/1.3.13/$injector/modulerr?p0=app&p1=Error%3A%20…(http%3A%2F%2Flocalhost%3A63342%2Fwww%2Flib%2Fionic.bundle.js%3A10158%3A12)**
actually I check
when I remove one script from index.html it remove that error
script is this
<script src="js/stationselect/router.js"></script>
But when I include again it show again given error
I don't what thing I write wrong ? Actually in my app there is some directory so I am not able to use fiddle .I will share my code with you
https://dl.dropbox.com/s/l2dtrxmnsurccxt/www.zip?dl=0
here is my script code
(function(){
'use strict'
angular.module('app.stationselect').config(routeChange);
routeChange.$inject=['$stateProvider','$urlRouterProvider'];
function routeChange($stateProvider, $urlRouterProvider){
$urlRouterProvider.state('selectstation',{
url:'/selectstation',
controller:'selectStation',
templateUrl:'js/stationselect/partial/home.html'
})
$urlRouterProvider.otherwise('/selectstation');
}
})();
Looks like you should have
angular.module('app.stationselect', ['ui.router']).config(...)
if you're creating the app.stationselect module there.
When you only provide one argument to angular.module() (ie, the module name), Angular attempts to retrieve it from the module registry.
If you've already created your module elsewhere, ignore the above.
Also, .state is a method of $stateProvider, not $urlRouterProvider. This is what's causing the error message.

how to remove the module error while including?

Uncaught Error: [$injector:nomod] Module 'firstApp.seconddir' 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
here is my code
http://goo.gl/LOcYI2
I am getting error while including the module in our project.
You should register new module before you use it.
Setting module: angular.module('your new module name', [])
Getting module: angular.module('module name')
So in your case, change the following line and error will not come.
angular.module('firstApp.seconddir').config(Routes);
to
angular.module('firstApp.seconddir', []).config(Routes);
Working demo: http://goo.gl/RvHzqr

Resources