Compare objects in Angular

前端 未结 4 1629
情歌与酒
情歌与酒 2020-12-04 13:59

Is it possible to do a \"deep\" comparison of two object in Angular? What I would like to do is compare each key/value pair. For example:

Object 1

{
         


        
4条回答
  •  盖世英雄少女心
    2020-12-04 15:01

    I know it's kinda late answer but I just lost about half an hour debugging cause of this, It might save someone some time.

    BE MINDFUL, If you use angular.equals() on objects that have property obj.$something (property name starts with $) those properties will get ignored in comparison.

    Example:

    var obj1 = {
      $key0: "A",
      key1: "value1",
      key2: "value2",
      key3: {a: "aa", b: "bb"}
    }
    
    var obj2 = {
      $key0: "B"
      key2: "value2",
      key1: "value1",
      key3: {a: "aa", b: "bb"}
    }
    
    angular.equals(obj1, obj2) //<--- would return TRUE (despite it's not true)
    

提交回复
热议问题