how to install underscore.js in my angular application? - angularjs

I used yo-angular to generate my angularjs template with bootstrap/grunt/bower. I also want to use underscore in the app:
npm install underscore --save-dev
In the MainCtrl I am calling underscore.js just to see whether it works:
angular.module('yomanApp')
.controller('MainCtrl', function ($scope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'AngularJS'
];
_.each([1,2,3],console.log);
});
When I run the application with Chrome I get this errmsg in the console:
ReferenceError: _ is not defined
at new <anonymous> (http://localhost:9000/scripts/controllers/main.js:18:5)
at invoke (http://localhost:9000/bower_components/angular/angular.js:4203:17)
at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:4211:27)
at http://localhost:9000/bower_components/angular/angular.js:8501:28
at link (http://localhost:9000/bower_components/angular-route/angular-route.js:975:26)
at invokeLinkFn (http://localhost:9000/bower_components/angular/angular.js:8258:9)
at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:7768:11)
at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:7117:13)
at publicLinkFn (http://localhost:9000/bower_components/angular/angular.js:6996:30)
at $get.boundTranscludeFn (http://localhost:9000/bower_components/angular/angular.js:7135:16) <div ng-view="" class="ng-scope">
After this error I added the module to the app config:
'use strict';
/**
* #ngdoc overview
* #name yomanApp
* #description
* # yomanApp
*
* Main module of the application.
*/
angular
.module('yomanApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'underscore'
])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'AboutCtrl'
})
.when('/accordeon', {
templateUrl: 'views/accordeon.html',
controller: 'IssuesCtrl'
})
.otherwise({
redirectTo: '/'
});
});
Now I am getting this error:
Uncaught Error: [$injector:modulerr] Failed to instantiate module yomanApp due to:
Error: [$injector:modulerr] Failed to instantiate module underscore due to:
Error: [$injector:nomod] Module 'underscore' 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.
Last thing I tried was adding it to the index.html:
<script src="node_modules/underscore/underscore.js"></script>
This results in the same error as above. Also get a 404 for the underscore.js?? Is this a grunt configuration issue or anything else?

I tend to just use a constant for this type of thing. It's a simple approach and allows you to explicitly state your dependencies in your application.
Install with bower:
bower install underscore --save
Load the library before angular:
<script src="bower_components/underscore/underscore.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="app/scripts/app.js"></script>
Define it as a constant (in app/scripts/app.js for example):
application.constant('_',
window._
);
Then in your controllers/services:
application.controller('Ctrl', function($scope, _) {
//Use underscore here like any other angular dependency
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
$scope.names = _.pluck(stooges, 'name');
});

Create a module with the name of underscore a module and then you can pass it in your application and it will be accessible. Currently the underscore module is undefined and hence you are getting this errror.
Your app becomes like this:
var underscore = angular.module('underscore', []);
underscore.factory('_', function() {
return window._; //Underscore should be loaded on the page
});
angular
.module('yomanApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'underscore'
])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'AboutCtrl'
})
.when('/accordeon', {
templateUrl: 'views/accordeon.html',
controller: 'IssuesCtrl'
})
.otherwise({
redirectTo: '/'
});
})
.controller('MainCtrl', function ($scope, _) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'AngularJS'
];
_.each([1,2,3],console.log);
});

Here's how you do it:link
You basically need to add angular underscore module which acts as a bridge between the two.

Related

Angular error, in npm build [$injector:nomod] Module 'testApp' is not available

Studying and practicing about npm build, im trying to compile my script using npm run build and i got an error
[$injector:nomod] Module 'testApp' 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.
any advice. thanks
app.js
import angular from 'angular';
import 'angular-ui-router';
import './services/router.js';
angular.module('testApp', [
'ui-router'
]);
-----------------------------
my roter.js
'use strict';
angular
.module('testApp')
.config([
'$urlRouterProvider',
'$stateProvider',
'$locationProvider',
function($urlRouterProvider, $stateProvider, $locationProvider) {
$locationProvider.html5Mode(false).hashPrefix('');
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
templateUrl: 'components/views/homepage.html',
controller: 'productController'
})
.state('about', {
url: '/about',
templateUrl: 'components/views/about.html'
})
.state('product', {
url: '/product/:id/:name',
templateUrl: 'components/views/product.html',
controller: 'productViewController'
})
}]);
Change
From
angular.module('testApp', [
'ui-router'
]);
To
angular.module('testApp', [
'ui.router'
]);

module was created but never loaded angularjs

I have read just about all the related responses to this question including this Why is my AngularJS module never loaded? and a few others and some how I am just not getting it right
this is what my app.js looks like
'use strict';
var appAcademia = angular.module("appAcademia", ['seblucas.slPageSizeChanger', 'LocalStorageModule', 'ngRoute', 'ngAnimate', 'ui.bootstrap', 'ui.router', 'ngResource']).config(function ($routeProvider, $stateProvider, $urlRouterProvider, $httpProvider, $resourceProvider) {
$httpProvider.interceptors.push('authInterceptorService');
$resourceProvider.defaults.stripTrailingSlashes = false;
$routeProvider.when('/home',
{
templateUrl: '/Home/Home',
controller: 'HomeController'
});
$routeProvider.when('/management',
{
templateUrl: '/management',
controller: 'ManagementController',
controllerAs: 'vm',
secure: true
});
$routeProvider.when('/createschoolmanagement',
{
templateUrl: '/management/createSchoolManagement',
controller: 'ManagementController',
controllerAs: 'vm',
secure: true
});
$routeProvider.when('/getschoolmanagement',
{
templateUrl: '/management/GetSchoolManagement',
controller: 'ManagementController',
controllerAs: 'vm',
secure: true
});
$routeProvider.when('/login',
{
templateUrl: '/home/login',
controller: 'LoginController'
});
$routeProvider.when('/signup',
{
templateUrl: '/home/signup',
controller: 'SignupController'
});
$routeProvider.when('/welcome',
{
templateUrl: '/home/welcome',
controller: 'WelcomeController'
});
$routeProvider.when('/users',
{
templateUrl: '/home/users',
controller: 'UsersController',
controllerAs: 'vm',
secure: true
});
//$routeProvider.when('/roles/:userId',
// {
// templateUrl: '/home/roles',
// controller: 'UserEditController',
// controllerAs: 'vm',
// secure: true
// });
$routeProvider.when('/createuser/:userId', {
controller: 'UserEditController',
templateUrl: '/home/EditUser',
controllerAs: 'vm',
secure: true //This route requires an authenticated user
});
$routeProvider.otherwise(
{
redirectTo: '/home'
});
});
var authServiceBase = 'http://localhost:26264/';
var resourceServiceBase = 'http://localhost:47039/';
appAcademia.constant('ngAuthSettings', {
apiAuthServiceBaseUri: authServiceBase,
apiResourceServiceBaseUri: resourceServiceBase,
clientId: '414e1927a3884f68abc79f7283837fd1'
});
appAcademia.run(['authService', function (authService) {
authService.fillAuthData();
}]);
All the modules below are created but never loaded
Then I get the following error message in the console
"Uncaught Error: [$injector:nomod] Module 'appAcademia' 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.3.0-build.3042+sha.76e57a7/$injector/nomod?p0=appAcademia"
when I run CCleaner and run the application again I get this error below
Uncaught Error: [$injector:nomod] Module 'appAcademia' 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=appAcademia
Now watch the Url in both cases. The first one refers to angular 1.3.0 and the second one refers the version 1.4.8 which is the one i actually installed
i have tried out the best practices all to no avail. I have been at this for weeks.
Modules (63)
The best practice for module names is to use dot.case or lowerCamelCase. Check the name of "LocalStorageModule".
Module "ngRoute" was created but never loaded.
Module "ngAnimate" was created but never loaded.
Module "ngResource" was created but never loaded.
Module "LocalStorageModule" was created but never loaded.
Module "ui.router.util" was created but never loaded.
Module "ui.router.router" was created but never loaded.
Module "ui.router.state" was created but never loaded.
Module "ui.router" was created but never loaded.
Module "ui.router.compat" was created but never loaded.
Module "appAcademia" was created but never loaded.
OK, now that I looked at your code, I understand your issue. It's with this line
var appAcademia = angular.module("appAcademia",
['seblucas.slPageSizeChanger', 'LocalStorageModule', 'ngRoute', 'ngAnimate',
'ui.bootstrap', 'ui.router', 'ngResource']).config(function ($routeProvider, $stateProvider, $urlRouterProvider, $httpProvider, $resourceProvider)
You're setting the variable to the module name's config function. Instead do this
var appAcademia = angular.module("appAcademia",
['seblucas.slPageSizeChanger', 'LocalStorageModule', 'ngRoute', 'ngAnimate',
'ui.bootstrap', 'ui.router', 'ngResource']);
appAcademia.config(function ($routeProvider, $stateProvider, $urlRouterProvider, $httpProvider, $resourceProvider)

Why does Yeoman scaffolding for AngularJS use the same module for each tab

I'm new to AngularJS and setting up my projects with Yeoman. Yeoman generates a scaffold for a basic AngularJS tab nav web site controlling tab content in the following way.
app.js:
'use strict';
angular
.module('ang1050315App', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'formModule'
])
.controller('HeaderController', ['$scope',
}]).
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'AboutCtrl'
})
.when('/contact', {
templateUrl: 'views/contact.html',
controller: 'ContactCtrl'
})
.otherwise({
redirectTo: '/'
});
});
And then the module content for each tab is basically the same, for example.
about.js:
'use strict';
angular.module('ang1050315App')
.controller('AboutCtrl', function ($scope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
});
html template:
<div class="collapse navbar-collapse" ng-controller="HeaderController">
<ul class="nav navbar-nav">
<li ng-class="{ active: isActive('/')}">Home</li>
<li ng-class="{ active: isActive('/about')}">About</li>
<li ng-class="{ active: isActive('/contact')}">Contact</li>
</ul>
</div>
What I'm wondering is, why use the same name module for each tab i.e. "ang1050315App". This seems like a disadvantage because it forces each tab to have the same module dependencies.
The module name, in this case: ang1050315App, is just an identifier for your entire app. All that's happening in your generated code is that each controller is being assigned to the specific app module (ang1050315App). In other words, "the controller 'AboutCtrl' is part of the project with the module name 'ang1050315App'".
If you wanted your code to be a bit less verbose, you could reference your Angular project using a variable instead (e.g. myApp):
var myApp = angular.module('ang1050315App', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'formModule'
]);
And for your controllers:
myApp.controller('AboutCtrl', function ($scope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
});
Notice how you no longer have to refer to the module name from the controller.
You can just assign yr angular app name to a variable and export it.
If you dont export "app", jshint will show error inside yr other js files.
'use strict';
/* global app:true */
/* exported app */
/**
* #ngdoc overview
* #name yrAppName
* #description
* # yrAppName
*
* Main module of the application.
*/
var app = angular.module('yrAppName', []);
then inside yr controller, directive or service...
somecontroller.js
app.controller('controllerName', []);
somefactory.js
app.factory('factoryName',[]);

angular js definition issues on config declaration

I want to know why my config function is not injected properly. this is my module injection code
angular.module("myApp", ['ngRoute'])
.controller('myCtrl', myCtrl)
.controller('common', common)
.config('configuration', configuration)
.factory('cprPostService', cprPostService);
this is my config declaration I get the following error
[$injector:modulerr] http://errors.angularjs.org/1.2.13/$injector/modulerr?p0=myApp&p1=Error%3A%20%5Bng%3Aareq%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.2.13%2Fng%2Fareq%3Fp0%3Dfn%26p1%3Dnot%2520a%2520function%252C%2520got%2520string%0A%20%20%20at%20yb%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A18%3A354)%0A%20%20%20at%20Qa%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A18%3A447)%0A%20%20%20at%20jc%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A28%3A333)%0A%20%20%20at%20d%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A30%3A291)%0A%20%20%20at%20Anonymous%20function%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A29%3A294)%0A%20%20%20at%20r%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A7%3A278)%0A%20%20%20at%20e%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A29%3A115)%0A%20%20%20at%20ac%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A32%3A230)%0A%20%20%20at%20c%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A17%3A429)%0A%20%20%20at%20%24b%20(https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.13%2Fangular.min.js%3A18%3A133)
function configuration($routeProvider, $locationProvider) {
$routeProvider
.when('/List', {
templateUrl: './pages/templates/initialization.html',
controller:'myCtrl'
})
.otherwise({
redirectTo: '/List'
});
};
You need to specify a $inject array for the config fuction:
configuration.$inject = ['$routeProvider', '$locationProvider'];
Actually the error line is
.config('configuration', configuration)
it has to be just,
.config(configuration)

Failed to instantiate module maq

I can't figure out what I'm doing wrong here.
//app.js
'use strict';
var app = angular.module('maq', [
'ui.router',
'maq.home.controller'
]).run([
'$rootScope',
'$state',
'$stateParams', function($rootScope, $state, $stateParams){
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}])
.config([
'$stateProvider',
'$urlRouterProvider', function($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
templateUrl: 'views/home.html',
controller: 'maq.home.controller'
})
}]);
//controller
'use strict';
app
.controller('maq.home.controller', [function(){
}]);
//error
Uncaught Error: [$injector:modulerr] Failed to instantiate module maq due to:
Error: [$injector:modulerr] Failed to instantiate module maq.home.controller due to:
Error: [$injector:nomod] Module 'maq.home.controller' 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.
You actually do not have a module called maq.home.controller Instead your registration is in such a way that it is the controller name.
If you want to separate your controller to another module (as what you are trying to do)
try:-
angular.module('maq.controller', []); //<-- Module creation
//...
angular.module('maq.controller')
.controller('HomeController', [function() { //<-- Controller registration
}]);
and in your app:-
var app = angular.module('maq', [
'ui.router',
'maq.controller' //<-- Controller mmodule
]);
Or just remove the dependency on the module that does not exist:-
angular.module('maq', [
'ui.router',
//'maq.home.controller' <-- Remove this
])
You are trying to depend on another module named maq.home.controller when it is not a module, it is a controller. You do not need to list controllers as dependencies in your module declaration. So just take that dependency out like so:
var app = angular.module('maq', [
'ui.router'
])

Resources