David Dal Busco
David Dal Busco

Reputation: 8672

angular.isUndefined & angular.equals in angular 2

I just began to try to migrate from angular 1 to 2, actually from ionic 1 to 2, and one of my first compilation error is that angular.isUndefined and angular.equals aren't resolved.

How should I fix that, any tips?

namespace Utils {

export class Comparator {

    static isEmpty(obj:any):boolean {
        return obj == null || Object.keys(obj).length === 0;
    }

    static isNumberNullOrZero(obj:number):boolean {
        return obj == null || obj === 0;
    }

    static equals(obj1:any, obj2:any):boolean {
        return angular.equals(obj1, obj2); // <== here angular unresolved
    }

    static isStringEmpty(str:string):boolean {
        return !str || 0 === str.length;
    }

    static isStringBlank(str:string):boolean {
        return !str || /^\s*$/.test(str);
    }

    static isBiggerThanZero(num:number):boolean {
        return num != null && !angular.isUndefined(num) && num > 0; // <== here angular unresolved
    }

  }
}

Upvotes: 4

Views: 12195

Answers (4)

Lance
Lance

Reputation: 885

replace angular.isUndefined with isUndefined, and add the function below for your code

```js

function isUndefined(o) {
    return typeof o === 'undefined';
}

```

Upvotes: 0

yeraycaballero
yeraycaballero

Reputation: 1603

You probably has already fixed it, but if you were wondering why those methods are not in angular2, here is why.

As you may know, angular team prefixed object names provided by the framework with $. That was a naming convention in angular1 to avoid collisions with the application code.

It turned out that they also needed to implement some methods like equals to ignore those prefixed properties. And that is one of the things that angular.equals does. If you compare objects using angular.equals those prefixes are ignored.

angular.equals({a: 1}, {a: 1}) === true
angular.equals({a: 1}, {a: 1, $a: 2}) === true

Find more in this post

Upvotes: 2

Piyush.kapoor
Piyush.kapoor

Reputation: 6803

Angular2 dont have isUndefined

You can simply replace angular.isUndefined(num) by

if (!num) {
}

Upvotes: -4

hholtij
hholtij

Reputation: 2936

In your case you can simply replace angular.isUndefined(num) with num === undefined.

Object equality is a bit trickier. There are libraries that include such functionality, e.g. Lodash

Upvotes: 4

Related Questions