請指導我如何在沒有工廠的情況下將下劃線注入控制器。AngularJS:如何在沒有工廠的情況下將下劃線注入控制器
與工廠我的代碼示例正在運行....在這裏。
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);
});
我錯過了一些東西....請告訴我我需要糾正的代碼。謝謝
看我複製你的代碼,但不工作在這裏是jsfiddle https://jsfiddle.net/tridip/3emgttcg/1/ –
我在這裏使用了不同名稱的控制器,我修復了這個問題,代碼工作時沒有注入下劃線。這裏是js小提琴版本https://jsfiddle.net/tridip/3emgttcg/2/ –