简体   繁体   English

如何将jQuery注入Angular 1.5组件

[英]How to inject jQuery into an Angular 1.5 component

I was wondering how to inject jquery into a component. 我想知道如何将jquery注入组件。 For example 例如

In my webpack.conf file: 在我的webpack.conf文件中:

 new webpack.ProvidePlugin({
  $: "jquery",
  jQuery: "jquery",
  "window.jQuery": "jquery"
 })

In my component file: 在我的组件文件中:

module.exports = {
 templateUrl: 'app/example.html',
 controller: ['$', exampleController]
};

function exampleController(){
 console.log('exampleController');
};

Stack: Angular 1.5, JavaScript, NPM and Webpack 堆栈:Angular 1.5,JavaScript,NPM和Webpack

Unless you have jQuery wrapped as an angular module, you do not inject it into a controller using Angular injection. 除非您将jQuery包装为angular模块,否则不要使用Angular注入将其注入到控制器中。 In order to use jQuery inside an angular component with webpack, simply require it at the top of whatever file you are using it: 为了在webpack的有角度组件中使用jQuery,只需在使用它的任何文件的顶部都要求它:

var $ = require('jquery');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM