简体   繁体   English

如何将window.prototype function in JavaScript to angular

[英]How to window.prototype function in JavaScript to angular

I am converting angularJs code to angular code.我正在将 angularJs 代码转换为 angular 代码。 I have a function in JavaScript. I want to use that in an angular component.我在 JavaScript 中有一个 function。我想在 angular 组件中使用它。 I am using angular version 12+我正在使用 angular 版本 12+

The JavaScript is the below code: JavaScript 是以下代码:

(function(window) {
var nodes = [];
window.JSONLoop = function(obj, idPropertyName, childrenPropertyName) {
  this.id = idPropertyName;
  this.children = childrenPropertyName;
  this.count = 0;
  this.countNodes(obj);
  this.total = this.count + 0;
}

window.JSONLoop.prototype = {
  constructor: JSONLoop,
  isEmpty: function(obj) {
    for(var property in obj) {
      return false;
    }
    return true;
  },
  countNodes: function(obj) {
    var that = this;
    this.count++;
    return function() {
      if (!obj || that.isEmpty(obj)) {
        return false;
      } else {
        if (obj[that.children]) {
          obj[that.children].forEach(function(child) {
            that.countNodes(child);
          });
        }
      }
    }();
  },
  findNodeById: function(obj, id, callback) {
    if (obj[this.id] === id) {
      this.count = this.total + 0;
      callback(null, obj);
    } else {
      if (this.count === 1) {
        this.count = this.total + 0;
        callback('the node does not exist', null);
      }
      this.count--;
      if (obj[this.children]) {
        var that = this;
        obj[this.children].forEach(function(node) {
          that.findNodeById(node, id, callback);
        });
      }
    }
  }, 
}}(window));

In existing angularjs code,the above function is used like this,在现有的angularjs代码中,上面的function是这样使用的,

 var jsonloop = new JSONLoop(data[0],'id', 'childBeans');
 jsonloop.findNodeById(data[0], dataId.substr(2), function(err, node) {
                      if (err) {
                        childItemObj="";
                      } else {
                        childItemObj=node;
                      }
                    });

Sample JSON-示例 JSON-

{
hierarchy: "3"
hierarchyStr: "product"
id: "1730637"
levelId: null
parentId: "0"
smartBeans: Array(4) 
0: {hierarchy: '3.3', hierarchyStr: 'product~@product-1', id: '1730637-1730638',  …}
1: {hierarchy: '3.4', hierarchyStr: 'product~@product-2', id: '1730637-1730670',  …}
2: {hierarchy: '3.5', hierarchyStr: 'product~@product-3', id: '1730637-1730686',  …}
3: {hierarchy: '3.6', hierarchyStr: 'product~@product-4', id: '1730637-1730696',  …}
[[Prototype]]: Object
}

Able to covert the normal functions to angular. But this one has window.prototype.能够将正常函数转换为 angular。但是这个有 window.prototype。 How to convert or use that in angular. I am not sure how to covert the js function of function code to angular and use.如何在 angular 中转换或使用它。我不确定如何将 function 代码的 js function 转换为 angular 并使用。 Any suggestions will be really helpful.任何建议都会非常有帮助。 Thanks!谢谢!

You need to use the es6 version of the same package which is json-digger Below is a working stackblitz example for your reference!您需要使用相同的 package 的es6版本,即json- digger 下面是一个工作 stackblitz 示例,供您参考!

json-digger stackblitz json-digger 堆栈闪电战

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM