Angular - 自定义json过滤器

时间:2014-12-20 11:36:08

标签: javascript json angularjs filter

我正在尝试自定义json过滤器以在其中添加链接。我已经看过它是如何构建在源代码中并试图摆弄它。 我们的想法是在项目键上添加链接,如下所示。我知道这不起作用,但我完全不知道如何继续下去。

;(function() {
angular.module('app', [])

  .controller('testCtrl', [ '$scope', function($scope) {
    $scope.data = {
      name: 'bob',
      lastname: 'dylan'
    }
  }])

  .filter('customJson', function () {
    function toJsonReplacer(key, value) {
      if (typeof value === "string")
        return '<a href="http://test.com/' + vaue; '" >' + value + '</a>';

      return value;
    }

    function toJson(obj, pretty) {
      if (typeof obj === 'undefined') { return undefined; }
      return JSON.stringify(obj, toJsonReplacer, pretty ? '  ' : null);
    }
    return function(object) {
      return toJson(object, true);
    };
  });
})();

有没有人有想法?

0 个答案:

没有答案