加上棱角分明$资源的自定义函数 [英] add a custom function on angular $resource

查看:86
本文介绍了加上棱角分明$资源的自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是这样的一个角资源

I have an angular resource that goes something like this

app.factory('User', function ($resource) {
    return $resource(
        '/api/user/:listCtrl:id/:docCtrl/', {
            id: '@id',
            listCtrl: '@listCtrl',
            docCtrl: '@docCtrl'
        }, {
            update: {
                method: 'PUT'
            },
            current: {
                method: 'GET',
                params: {
                    listCtrl: 'current'
                }
            },
            nearby: {
                method: 'GET',
                params: {
                    docCtrl: 'nearby'
                },
                isArray: true
            }
        }
    );
});

现在我想在视图中拥有的全名,我可以添加一个方法,这样,当我做到这一点。

now i want to have full name in the view, can i add a method so that when i do this

$ scope.user =用户()电流();

$scope.user = User().current();

而不是做以下的HTML

instead of doing the following in html

<p>{{ user.first_name }} {{ user.last_name }}</p>

我这样做

<p>{{ user.get_full_name }}</p>

有没有办法给此属性添加到$资源?

is there a way to add this property to the $resource?

推荐答案

打了具有角一段时间后,我觉得一个客户过滤器将是一个更为明智的做法,而不是在$资源添加一个属性。

after playing for a while with angular, I think a customer filter would be a much wiser approach, rather than adding a property on $resource.

下面是如何做一个过滤器

here is how to do a filter

app.filter('getFullName', function () {
    return function (input) {
      if (input) {
        return input.first_name + ' ' + input.last_name;
      } else {
        return 'default';
      }
    };
  });

这篇关于加上棱角分明$资源的自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆