AngularJS: 如何在没有工厂的情况下将下划线注入控制器
AngularJS: How to inject underscore into controller without factory
请指导我如何在没有工厂的情况下将下划线注入控制器。
对于工厂,我的代码示例是 运行...在这里。
var myApp = angular.module('myApp', []);
myApp.factory('_', function() {
return window._; //Underscore should be loaded on the page
});
myApp.controller('MainCtrl', function ($scope, _) {
});
但是当我尝试在控制器中注入下划线然后得到如下错误时没有工厂
SyntaxError: missing ) after argument list Error: [$injector:modulerr]
Failed to instantiate module myApp due to: [$injector:nomod] Module
'myApp' 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.
这里是没有工厂将下划线注入控制器的代码。
<div ng-app="myApp" ng-controller="MainCtrl">
</div>
var myApp = angular.module('myApp' , ['underscore']);
myApp.controller('MyCtrl', function ($scope, _)
{
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS2',
'AngularJS1'
];
var x = _.last($scope.awesomeThings, 2);
_.each(x.reverse(), alert);
});
我遗漏了一些东西....请告诉我我需要在代码中纠正什么。谢谢
依赖注入应该只用于 angular 东西(服务、控制器等)。
所有与 angular 无关的东西都不应该与依赖注入一起使用。
只要在 angular 控制器之前加载下划线,您就可以使用它,因为它会被添加到 window 对象中。
以下将毫无问题地工作:
var myApp = angular.module('myApp' , []);
myApp.controller('MyCtrl', function ($scope)
{
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS2',
'AngularJS1'
];
var x = _.last($scope.awesomeThings, 2);
_.each(x.reverse(), alert);
});
你可以像这样为下划线创建一个单独的模块
var underscore = angular.module('underscore', []);
underscore.factory('_', ['$window', function($window) {
return $window._; // assumes underscore has already been loaded on the page
}]);
现在将下划线模块注入您的应用程序模块
var myApp = angular.module('myApp' , ['underscore']);
myApp.controller('MyCtrl', function ($scope, _)
{
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS2',
'AngularJS1'
];
var x = _.last($scope.awesomeThings, 2);
_.each(x.reverse(), alert);
});
请指导我如何在没有工厂的情况下将下划线注入控制器。
对于工厂,我的代码示例是 运行...在这里。
var myApp = angular.module('myApp', []);
myApp.factory('_', function() {
return window._; //Underscore should be loaded on the page
});
myApp.controller('MainCtrl', function ($scope, _) {
});
但是当我尝试在控制器中注入下划线然后得到如下错误时没有工厂
SyntaxError: missing ) after argument list Error: [$injector:modulerr] Failed to instantiate module myApp due to: [$injector:nomod] Module 'myApp' 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.
这里是没有工厂将下划线注入控制器的代码。
<div ng-app="myApp" ng-controller="MainCtrl">
</div>
var myApp = angular.module('myApp' , ['underscore']);
myApp.controller('MyCtrl', function ($scope, _)
{
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS2',
'AngularJS1'
];
var x = _.last($scope.awesomeThings, 2);
_.each(x.reverse(), alert);
});
我遗漏了一些东西....请告诉我我需要在代码中纠正什么。谢谢
依赖注入应该只用于 angular 东西(服务、控制器等)。
所有与 angular 无关的东西都不应该与依赖注入一起使用。
只要在 angular 控制器之前加载下划线,您就可以使用它,因为它会被添加到 window 对象中。
以下将毫无问题地工作:
var myApp = angular.module('myApp' , []);
myApp.controller('MyCtrl', function ($scope)
{
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS2',
'AngularJS1'
];
var x = _.last($scope.awesomeThings, 2);
_.each(x.reverse(), alert);
});
你可以像这样为下划线创建一个单独的模块
var underscore = angular.module('underscore', []);
underscore.factory('_', ['$window', function($window) {
return $window._; // assumes underscore has already been loaded on the page
}]);
现在将下划线模块注入您的应用程序模块
var myApp = angular.module('myApp' , ['underscore']);
myApp.controller('MyCtrl', function ($scope, _)
{
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS2',
'AngularJS1'
];
var x = _.last($scope.awesomeThings, 2);
_.each(x.reverse(), alert);
});