Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i Check object.property is defined in angular 2?

Is there any equivalent function in angular 2 as angular.isDefined as in angular 1

Checked The safe navigation operator ?. which only supports in tempalte

like image 564
Rajath M S Avatar asked Nov 23 '16 13:11

Rajath M S


People also ask

What is the angular equivalent to an Angularjs watch?

You can use getter function or get accessor to act as watch on angular 2.

What does means in angular?

Means safe navigation operator. From Docs. The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths.


3 Answers

Typescript does NOT have a function to check if a variable is defined, neither does Angular2.

Using a juggling-check, you can test both null and undefined in one hit:

if (object.property == null) {

If you use a strict-check, it will only be true for values set to null and won't evaluate as true for undefined variables:

if (object.property === null) {
like image 124
Lucas Avatar answered Oct 25 '22 06:10

Lucas


no

angular2 uses proper JavaScript. it moves away from angular specific language. that is one of its goals.

just do ..

if (something == null) { // the only exception where double equals is OK - checks for undefined or null
    ...
}

Typescript does have a !. operator which might be worth looking at, not exactly sure how it works but i think it is similar to ?.

like image 33
danday74 Avatar answered Oct 25 '22 06:10

danday74


you could use undefined

function getSchool(name: string, address?: string, pinCode?: string): string {
            if (address == undefined) {
                console.log('address not defined');
            }
            if (pinCode == undefined) {
                console.log('pincode not defined');
            }
}
like image 40
user511564 Avatar answered Oct 25 '22 07:10

user511564