使用Codekit时未定义'angular'

时间:2015-07-12 02:23:25

标签: javascript angularjs codekit

每次保存myApp.js时,CodeKit都会给我一个警告。当我通过CodeKit预览加载index.html时,firstNamelastName变量显示正常。我只想让这个问题消失。

"var app = angular.module("myApp", []);
'angular' is not defined"

以下是我的两个文件index.htmlmyApp.js

的index.html:

<html>
<script src="angular/angular.js"></script>
<script src="myApp.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
    <div>
        {{ firstName + " " + lastName }}
    </div>
</body>
</html>

myApp.js:

var app = angular.module("myApp", []);

app.controller("myCtrl", function($scope) {
    $scope.firstName    = "John";
    $scope.lastName= "Doe";
});

2 个答案:

答案 0 :(得分:4)

我没有尝试过Claies的解决方案,但我们在项目中所做的只是将其注册为全局,以便Codekit不会抱怨。只有在确定已定义角度时才执行此操作,否则会隐藏错误。

选项1 - 适用于jshint

将以下内容放入JS文件中:

collect

参考:https://incident57.com/codekit/help.html#jshint

选项2 - 适用于jshint和jslint

或者,您可以在Codekit配置中定义全局变量:

  1. 在Codekit中,打开“项目设置”
  2. 选择'Syntax Checkers'
  3. 在“自定义全局”文本字段中,您可以输入逗号分隔的变量和函数列表,您知道这些变量和函数是在JS之外的其他地方定义的 脚本。
  4. 参考:https://incident57.com/codekit/help.html#jslint

答案 1 :(得分:1)

这是由于Code Linting进程正在主动搜索语法错误而发生的。不幸的是,它没有意识到angular您的脚本文件可用的全局变量,因为它是在不同的文件中声明的。它实际上在浏览器(或预览窗格)中工作,但Linting不知道angular的存在。

您可以在JavaScript文件的顶部添加/// <reference path="angular/angular.js" />,以通知语法检查器实际声明angular变量的位置。