¿Cómo instalar underscore.js en mi aplicación angular?
Utilicé yo-angular para generar mi plantilla angularjs con bootstrap / grunt / bower. También quiero usar subrayado en la aplicación:
npm install underscore --save-dev
En MainCtrl, estoy llamando a underscore.js solo para ver si funciona:
angular.module('yomanApp')
.controller('MainCtrl', function ($scope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'AngularJS'
];
_.each([1,2,3],console.log);
});
Cuando ejecuto la aplicación con Chrome, obtengo este error en la consola:
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">
Después de este error, agregué el módulo a la configuración de la aplicación: 'use estricto';
/**
* @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: '/'
});
});
Ahora recibo este 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.
Lo último que intenté fue agregarlo al index.html:
<script src="node_modules/underscore/underscore.js"></script>
Esto da como resultado el mismo error que el anterior. También obtenga un 404 para el subrayado.js ?? ¿Es este un problema de configuración grave o algo más?