angular.js:14195 Error: [$resource:badmember] Dotted member path "@" is invalid

angular.js:14195 Error: [$resource:badmember] Dotted member path "@" is invalid

我正在使用 angular.resource js

我正在尝试访问具有从表单搜索触发的查询参数的获取服务。

如果用户在输入字段中仅使用“@”进行搜索,它将作为以“@”字符开头的查询参数,然后获得上述异常

提前致谢。

因为 angular.resource js 中的以下代码

// Helper functions and regex to lookup a dotted path on an object
// stopping at undefined/null.  The path must be composed of ASCII
// identifiers (just like $parse)
var MEMBER_NAME_REGEX = /^(\.[a-zA-Z_$@][0-9a-zA-Z_$@]*)+$/;

function isValidDottedPath(path) {
  return (path != null && path !== '' && path !== 'hasOwnProperty' &&
      MEMBER_NAME_REGEX.test('.' + path));
}

function lookupDottedPath(obj, path) {
  if (!isValidDottedPath(path)) {
    throw $resourceMinErr('badmember', 'Dotted member path "@{0}" is invalid.', path);
  }
  var keys = path.split('.');
  for (var i = 0, ii = keys.length; i < ii && angular.isDefined(obj); i++) {
    var key = keys[i];
    obj = (obj !== null) ? obj[key] : undefined;
  }
  return obj;
}

如果你真的想把它作为你的 url 参数。尝试转义 @ 字符,因为这在 $resource library

中具有特殊含义

通常情况下,当我们只有@作为值时,它会在请求正文中搜索“@”之后的字符串值。 因为它是一个 get 方法,所以 angular 资源会忽略它。非常奇怪的行为。

AngularJs 文档:https://code.angularjs.org/1.5.11/docs/api/ngResource/service/$resource

If the parameter value is prefixed with @, then the value for that parameter will be extracted from the corresponding property on the data object (provided when calling a "non-GET" action method). For example, if the defaultParam object is {someParam: '@someProp'} then the value of someParam will be data.someProp. Note that the parameter will be ignored, when calling a "GET" action method (i.e. an action method that does not accept a request body)

因此,在调用资源中的任何方法之前,请尝试转义 @ 符号:

paramValue = paramValue.replace(/@/gi, '\@');

同样,您可以在 $httpProvider 的拦截器服务的请求方法中发生 api 调用之前删除 scape。

configParams = configParams.replace(/\@/gi, '@');

如果需要更多帮助,请告诉我。

谢谢