将jquery和下划线注入角度js组件 [英] inject jquery and underscore to angular js component
本文介绍了将jquery和下划线注入角度js组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在新服务中使用angularjs,下划线和jQuery:
I am using angularjs, underscore and jQuery in my new service:
myModule.factory('MyService', ['MyResource', function (MyResource) {
....
// Here I make use of _ and $
}]);
如何向新服务注入下划线或jQuery,以便我可以确定_是下划线和$是jquery?
How can I inject underscore or jQuery to the new service so I can be sure that _ is underscore and $ is jquery?
我正在寻找类似的东西:
I am looking for something like:
myModule.factory('MyService', [ 'underscore', 'jquery','MyResource', function (_, $, MyResource) {
....
// Here I want to use $ and _ and be SURE that _ is underscore and $ is jquery
}]);
推荐答案
基于@ moderndegree的方法我实现了以下代码,我不能说这是完美的,但这样测试人员会知道它是否具有jQuery依赖性,因为 $ window
是太通用的对象注入。
based on @moderndegree's approach I have implemented the following code, I cannot say it is perfect but this way tester would know if it has jQuery dependency as $window
is too generic object to inject.
'use strict';
(function () {
var app= angular.module('app');
//these are just references the instance of related lib so we can inject them to the controllers/services in an angular way.
app.factory('jQuery', [
'$window',
function ($window) {
return $window.jQuery;
}
]);
app.factory('Modernizr', [
'$window',
function ($window) {
return $window.Modernizr;
}
]);
app.factory('Highcharts', [
'$window',
function ($window) {
return $window.Highcharts;
}
]);
})();
这篇关于将jquery和下划线注入角度js组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文