使用 jQuery jslint 错误:“$”未定义

muu*_*ess 1 jquery angularjs

我正在将 JQuery 与 Angular 一起使用。一切工作正常,除了我在 JSLint 中不断收到错误说'$' is not defined.. 我的控制器看起来像这样:

function controllerFunc($scope) {
    // Stuff goes in here
};
angular.module('core').controller('controllerName', ['$scope', controllerFunc]);
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

eli*_*ide 5

您可以在 .js 文件顶部添加此注释:

/*global $ */
Run Code Online (Sandbox Code Playgroud)

这告诉 JSLint 这$是在其他文件中定义的全局变量,因此它可以冷静下来。我发现每当我有一个依赖于 jQuery 的 JavaScript 文件时我都需要使用它。当您将代码分解为多个 .js 文件时,同样的原理也适用,如下所示:

// foo.js
function foo() {
    return 'Hello world!';
}


// bar.js
/*global foo */
function bar() {
    return foo();
}
Run Code Online (Sandbox Code Playgroud)