加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – ngResource中的虚拟属性

发布时间:2020-12-17 08:48:38 所属栏目:安全 来源:网络整理
导读:是否可以向ngResource添加虚拟属性? 我创建了这样的服务 app.factory('Person',['$resource',function($resource) { return $resource('api/path/:personId',{ personId: '@_id' },{ update: { method: 'PUT' } });}]) Person具有属性名称和属性姓氏. 我想
是否可以向ngResource添加虚拟属性?
我创建了这样的服务
app.factory('Person',['$resource',function($resource) {

  return $resource('api/path/:personId',{
    personId: '@_id'
      },{
        update: {
          method: 'PUT'
        }
      });
}])

Person具有属性名称和属性姓氏.
我想通过添加返回resource.name resource surname的虚拟属性fullname来检索fullname.
我知道我可以在控制器中添加它,但是将它添加到服务中会使它更加便携.
我试过这样的事

app.factory('Person',{
        update: {
          method: 'PUT'
        },fullname: function(resource){
      return resource.name + ' ' + resource.surname;
    }
  });
 });
}])

但它不起作用.

您可以尝试 intercepting来自Person资源的响应并增加响应.像这样:
app.factory('Person',function($resource) {
  function getFullName(){
      return this.name + ' ' + this.surname;
  };

  return $resource('api/path/:personId','get': {method:'GET',isArray:false,interceptor:{
              'response': function(response) {
                  response.fullname = getFullName; //augment the response with our function
                  return response;
               }
         }}
      });
}]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读