使用资源插件时出现奇怪的角度误差

时间:2015-02-12 00:54:47

标签: javascript angularjs

我的公共API资源服务,如下所述:

angular.module('fortiguard')
    .factory('API', ['$resource', 'Tools', function($resource, Tools) {

        var Posts = $resource('/api/posts', {
            categories: {method:'GET', isArray:true, url:'/api/posts/categories'}
        });

        var posts_categories = function(next) {
            Posts.categories(function(cats) {
                next( Tools.unlinkArray(cats) );
            });
        };

        return {
            getPostCategories : posts_categories
        };
    }]);

执行时抛出这个奇怪的错误

TypeError: undefined is not a function
    at Object.posts_categories [as getPostCategories] (http://localhost:3000/scripts/compiled.js:55101:21)
    at new <anonymous> (http://localhost:3000/scripts/compiled.js:54655:13)
    at invoke (http://localhost:3000/scripts/compiled.js:24236:17)
    at Object.instantiate (http://localhost:3000/scripts/compiled.js:24244:27)
    at http://localhost:3000/scripts/compiled.js:28513:28
    at link (http://localhost:3000/scripts/compiled.js:47824:26)
    at invokeLinkFn (http://localhost:3000/scripts/compiled.js:28270:9)
    at nodeLinkFn (http://localhost:3000/scripts/compiled.js:27780:11)
    at compositeLinkFn (http://localhost:3000/scripts/compiled.js:27129:13)
    at publicLinkFn (http://localhost:3000/scripts/compiled.js:27008:30) <ng-view class="ng-scope">

当我使用Chrome的工具进入违规行时,我很惊讶地发现它就是这一行(下面的注释行):

function consoleLog(type) {
  var console = $window.console || {},
      logFn = console[type] || console.log || noop,
      hasApply = false;

  // Note: reading logFn.apply throws an error in IE11 in IE8 document mode.
  // The reason behind this is that console.log has type "object" in IE8...
  try {
    hasApply = !!logFn.apply;
  } catch (e) {}

  if (hasApply) {
    return function() {
      var args = [];
      forEach(arguments, function(arg) {
        args.push(formatError(arg));
      });
      return logFn.apply(console, args);    // <------------- THIS LINE
    };
  }

  // we are IE which either doesn't have window.console => this is noop and we do nothing,
  // or we are IE where console.log doesn't have apply so we log at least first 2 args
  return function(arg1, arg2) {
    logFn(arg1, arg2 == null ? '' : arg2);
  };
}

在堆栈跟踪的上一个项目中,它说我的api工厂中有这行的问题

Posts.categories(function(cats) {

然而,一切都在我用这个代替的时候起作用:

angular.module('fortiguard')
    .factory('API', ['$resource', 'Tools', function($resource, Tools) {

        var Posts = $resource('/api/posts/categories', { <--- CHANGED THIS PATH TO BE THE SAME ONE AS CATEGORIES BELOW
            categories: {method:'GET', isArray:true, url:'/api/posts/categories'}
        });

        var posts_categories = function(next) {
            Posts.query(function(cats) {  <--- CHANGED THIS TO QUERY
                next( Tools.unlinkArray(cats) );
            });
        };

        return {
            getPostCategories : posts_categories
        };
    }]);

我在这里不知所措,我甚至从另一个网站上复制并粘贴了我做的确切,并且效果很好。

在这里使用Angular 1.3.13

bower.json:
  "dependencies": {
    "angular": "~1.3.13",
    "angular-resource": "~1.3.13",
    "angular-route": "~1.3.13",
    "angular-sanitize": "~1.3.13",
  }

1 个答案:

答案 0 :(得分:1)

您将actions对象作为第二个参数而不是第三个参数传递。

此:

var Posts = $resource('/api/posts', {
    categories: {method:'GET', isArray:true, url:'/api/posts/categories'}
});

应该是这样的:

var Posts = $resource('/api/posts', {}, {
    categories: {method:'GET', isArray:true, url:'/api/posts/categories'}
});

https://docs.angularjs.org/api/ngResource/service/$resource