Pin*_*aas 8 npm underscore.js angularjs gruntjs bower
我使用yo-angular生成带有bootstrap/grunt/bower的angularjs模板.我还想在应用程序中使用下划线:
npm install underscore --save-dev
Run Code Online (Sandbox Code Playgroud)
在MainCtrl中我调用underscore.js只是为了看它是否有效:
angular.module('yomanApp')
.controller('MainCtrl', function ($scope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'AngularJS'
];
_.each([1,2,3],console.log);
});
Run Code Online (Sandbox Code Playgroud)
当我使用Chrome运行应用程序时,我在控制台中获得了这个错误:
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">
Run Code Online (Sandbox Code Playgroud)
在此错误之后,我将模块添加到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: '/'
});
});
Run Code Online (Sandbox Code Playgroud)
现在我收到此错误:
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.
Run Code Online (Sandbox Code Playgroud)
我尝试的最后一件事是将它添加到index.html:
<script src="node_modules/underscore/underscore.js"></script>
Run Code Online (Sandbox Code Playgroud)
这导致与上述相同的错误.还得到一个404 for the underscore.js ?? 这是一个咕噜咕噜的配置问题还是其他什么?
djs*_*ner 16
我倾向于只为这类事物使用常数.这是一种简单的方法,允许您在应用程序中明确声明您的依赖项.
用凉亭安装:
bower install underscore --save
Run Code Online (Sandbox Code Playgroud)
在角度之前加载库:
<script src="bower_components/underscore/underscore.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="app/scripts/app.js"></script>
Run Code Online (Sandbox Code Playgroud)
将其定义为常量(app/scripts/app.js例如):
application.constant('_',
window._
);
Run Code Online (Sandbox Code Playgroud)
然后在你的控制器/服务中:
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');
});
Run Code Online (Sandbox Code Playgroud)
Jay*_*ram 14
创建一个具有模块名称的underscore模块,然后您可以在您的应用程序中传递它,它将是可访问的.目前,下划线模块未定义,因此您得到了这个错误.
你的应用就像这样:
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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11967 次 |
| 最近记录: |