检查Angular资源响应是否为空

cyb*_*bat 5 angularjs

我有一个返回承诺的服务.我想检查返回的值是否为空对象.我怎样才能做到这一点.我想我需要以某种方式从promise对象中提取返回的值.

这是我的资源:

app.factory('Auth', ['$resource',
  function($resource) {
    return $resource('/user/identify', {}, {
      identify: {
        url: '/user/identify'
      },
    });
  }
]);
Run Code Online (Sandbox Code Playgroud)

然后在服务中:

Auth.identify(function(data) {

  // This always passes since data contains promise propeties
  if (!_.isEmpty(data)) {

  }
});
Run Code Online (Sandbox Code Playgroud)

控制台数据日志给出:

Resource
  $promise: Object
  $resolved: true
  __proto__: Resource
Run Code Online (Sandbox Code Playgroud)

当对象不为空但我想要更通用的方法时,我可以检查预期的属性.

use*_*490 0

要解开返回值,您可以直接访问承诺:

Auth.identify()
    .$promise
    .then(function(data) {
      if (!_.isEmpty(data)) {
        // ...
      }
    });
Run Code Online (Sandbox Code Playgroud)