1

I am trying to merge two object in ionic2 via angular.merge(Obj1,Obj2). But I get error on save. How do I get access to the global angular object.

Here is the code I am using (from newApp\app\home.ts)

syncWithPortfolio(data){
  var ptfoliObject = this.portfolioObject;
  if(data){
      for(var x in data){
          angular.merge(ptfoliObject[x],data[x]);
      }
  }

The error on console: "newApp/app/pages/home/home.ts(181,15): Error TS2304: Cannot find name 'angular'."

Thanks

Aks
  • 13
  • 4
  • Ionic 2 is built Angular 2, but your code seems to be Angular 1 that's why its not working. – Mithun Pattankar Aug 01 '16 at 08:04
  • I just wanna merge two object in Ionic2, How do I do that ? – Aks Aug 01 '16 at 08:06
  • try this link http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically, since IONIC 2 is also regular javascript. you might work with it – Mithun Pattankar Aug 01 '16 at 08:08
  • I am already working on alternatives. I just want to know, how to access the good old angular.merge in ionic2. – Aks Aug 01 '16 at 08:18
  • in package.json, you need to add Angular 1.*, then do npm install & load them in app.ts. But I highly NOT recommend doing this, If you want to use merge, sort, filter, find etc such kind of functions then its best to use LODASH library. – Mithun Pattankar Aug 01 '16 at 08:22
  • Yeah, that is too much of overhead to access 'angular'. I will stick with alternative mentioned below in answer. – Aks Aug 01 '16 at 08:32

1 Answers1

0

The approach is to leverage what is provided by JavaScript itself. In your case, [Object.assign][1]. Here is a sample:

syncWithPortfolio(data){
  var ptfoliObject = this.portfolioObject;
  if(data){
    for(var x in data){
      Object.assign(ptfoliObject[x], data[x]);
    }
  }
}
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360