在AngularJS应用程序中查找错误行

k3r*_*n1c 14 debugging angularjs

我刚刚开始玩AngularJS,我在下面得到了错误.

Error: Argument '?' is not a function, got Object
at assertArg (http://localhost/angular/project/scripts/vendor/angular.js:1039:11)
at assertArgFn (http://localhost/angular/project/scripts/vendor/angular.js:1049:3)
at http://localhost/angular/project/scripts/vendor/angular.js:4802:9
at http://localhost/angular/project/scripts/vendor/angular.js:4384:17
at forEach (http://localhost/angular/project/scripts/vendor/angular.js:137:20)
at nodeLinkFn (http://localhost/angular/project/scripts/vendor/angular.js:4369:11)
at compositeLinkFn (http://localhost/angular/project/scripts/vendor/angular.js:4015:15)
at compositeLinkFn (http://localhost/angular/project/scripts/vendor/angular.js:4018:13)
at publicLinkFn (http://localhost/angular/project/scripts/vendor/angular.js:3920:30)
at update (http://localhost/angular/project/scripts/vendor/angular.js:14202:11) 
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是:有没有办法找到错误发生在.js文件中的行?我在angular.js文件中获取了引发异常的行号,但是有太多文件可能发生错误.

我尝试使用AngularJS Batarang,但这更适用于调试语义而不是语法错误.

谢谢.

小智 2

如果您链接到可能导致此错误的 js 文件,事情会更容易。

从 angular.js 源代码来看,https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js 看起来像是实例化控制器的问题。

这是导致断言失败的行:

/**
 * @ngdoc function
 * @name ng.$controller
 * @requires $injector
 *
 * @param {Function|string} constructor If called with a function then it's considered to be the
 *    controller constructor function. Otherwise it's considered to be a string which is used
 *    to retrieve the controller constructor using the following steps:
 *
 *    * check if a controller with given name is registered via `$controllerProvider`
 *    * check if evaluating the string on the current scope returns a constructor
 *    * check `window[constructor]` on the global `window` object
 *
 * @param {Object} locals Injection locals for Controller.
 * @return {Object} Instance of given controller.
 *
 * @description
 * `$controller` service is responsible for instantiating controllers.
 *
 * It's just a simple call to {@link AUTO.$injector $injector}, but extracted into
 * a service, so that one can override this service with {@link https://gist.github.com/1649788
 * BC version}.
 */
return function(constructor, locals) {
  if(isString(constructor)) {
    var name = constructor;
    constructor = controllers.hasOwnProperty(name)
        ? controllers[name]
        : getter(locals.$scope, name, true) || getter($window, name, true);
Run Code Online (Sandbox Code Playgroud)

======>assertArgFn(构造函数,名称,true); }

  return $injector.instantiate(constructor, locals);
};
Run Code Online (Sandbox Code Playgroud)

它无法找到控制器的构造函数。

  • 是的,你是对的,我省略了控制器名称`<div ng-controller=""`。但这并不是我问的真正原因。省略控制器名称的文件是 LoginWin.html,我很好奇是否有任何方法可以获得指向有错误的文件的错误消息。 (2认同)
  • 为什么没有人对此投赞成票?过去我从来不需要行号,因为我本质上知道我的代码会在哪里发生问题。然而,尝试将代码从一个系统集成到另一个系统时,我现在需要从控制台中的错误中获得更好的信息,而角度是无用的。 (2认同)