2017-06-20 45 views
0

我試圖在我的angularJS應用程序中使用JavaScript包。我已經添加了參考我的index.html不能使用深度差異JavaScript功能

<script src="deep-diff-0.3.1.min.js"></script> 

,我用我的控制器有例如:

var lhs = { 
    name: 'my object', 
    description: 'it\'s an object!', 
    details: { 
     it: 'has', 
     an: 'array', 
     with: ['a', 'few', 'elements'] 
    } 
}; 

var rhs = { 
    name: 'updated object', 
    description: 'it\'s an object!', 
    details: { 
     it: 'has', 
     an: 'array', 
     with: ['a', 'few', 'more', 'elements', { than: 'before' }] 
    } 
}; 

var differences = diff(lhs, rhs); 

但是,當我嘗試運行我的應用程序出現以下錯誤:

ReferenceError: diff is not defined 

我用盡指向所有不同版本的,我也試圖通過兩個亭子和NPM安裝

+0

它可以幫助你:https://stackoverflow.com/questions/264430/how-can-i-get-a-list-of-兩個JavaScript之間的差異 - 對象圖 –

回答

1

我認爲你必須使用你的代碼:

var diff = require('deep-diff').diff; 

您也可以使用函數波紋管做深做差異。我用它偉大工程:

var deepDiffMapper = function() { 
 
    return { 
 
     VALUE_CREATED: 'created', 
 
     VALUE_UPDATED: 'updated', 
 
     VALUE_DELETED: 'deleted', 
 
     VALUE_UNCHANGED: 'unchanged', 
 
     map: function (obj1, obj2) { 
 
      if (this.isFunction(obj1) || this.isFunction(obj2)) { 
 
       throw 'Invalid argument. Function given, object expected.'; 
 
      } 
 

 
      if (this.isValue(obj1) || this.isValue(obj2)) { 
 
       return { 
 
        type: this.compareValues(obj1, obj2), 
 
        data1: obj1, 
 
        data2: obj2 
 
       }; 
 
      } 
 

 
      var diff = {}; 
 
      for (var key in obj1) { 
 
       if (this.isFunction(obj1[key])) { 
 
        continue; 
 
       } 
 

 
       var value2 = undefined; 
 
       if ('undefined' != typeof (obj2[key])) { 
 
        value2 = obj2[key]; 
 
       } 
 

 
       var m = this.map(obj1[key], value2); 
 
       diff[key] = m; 
 
      } 
 

 
      for (var key in obj2) { 
 
       if (this.isFunction(obj2[key]) || ('undefined' != typeof (diff[key]))) { 
 
        continue; 
 
       } 
 

 
       var m = this.map(undefined, obj2[key]); 
 
       diff[key] = m; 
 
      } 
 

 
      return diff; 
 

 
     }, 
 
     compareValues: function (value1, value2) { 
 
      if (value1 === value2) { 
 
       return this.VALUE_UNCHANGED; 
 
      } 
 
      if ('undefined' == typeof (value1)) { 
 
       return this.VALUE_CREATED; 
 
      } 
 
      if ('undefined' == typeof (value2)) { 
 
       return this.VALUE_DELETED; 
 
      } 
 

 
      return this.VALUE_UPDATED; 
 
     }, 
 
     isFunction: function (obj) { 
 
      return {}.toString.apply(obj) === '[object Function]'; 
 
     }, 
 
     isArray: function (obj) { 
 
      return {}.toString.apply(obj) === '[object Array]'; 
 
     }, 
 
     isObject: function (obj) { 
 
      return {}.toString.apply(obj) === '[object Object]'; 
 
     }, 
 
     isValue: function (obj) { 
 
      return !this.isObject(obj) && !this.isArray(obj); 
 
     }, 
 
     changed: function (m) { 
 

 
      if (!this.isObject(m)) 
 
       return false; 
 

 
      var c = false; 
 
      var found = false; 
 

 
      angular.forEach(m, function (value, key) { 
 

 
       if (!found) { 
 

 
        if (deepDiffMapper.isObject(value)) 
 
         c = (c || deepDiffMapper.changed(value)); 
 
        else if (key == "type") { 
 
         c = (c || (value == "updated") || (value == "created") || (value == "deleted")); 
 
        } 
 

 
        if (c) { 
 
         found = true; 
 
        } 
 
       } 
 
      }); 
 

 
      return c; 
 
     } 
 
    } 
 
}();

+0

感謝您的回答,我確實需要但我得到了ReferenceError:require沒有定義 –

+0

您只能在Node.JS中使用它,因爲它在那裏定義。 – Vanderson

+0

我編輯了建議的功能,有一些自定義的代碼:D。前段時間我面對同樣的深層差異問題,經過一些研究後,我提出了這個解決方案。 – Vanderson