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具有属性名称和属性姓氏. 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; } }} }); }]); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |