Is it good practice to have angular-templates check scope-variables for "undefined"?

60 Views Asked by At

Example: The picture-gallery-directive retrieves pictures and passes them to the scope:

var galleryBootstrapData = bootstrapDataService.get('galleryBootstrapData');
$scope.galleryPictures = galleryBootstrapData.pictures;

The picture-gallery-template renders the pictures and as shown below the number of pictures:

<div ng-if="galleryPictures.length && galleryPictures.length>0" >{{galleryPictures.length}}</div>

Is it good practice to check the scope-variables for undefined from within the template?

1

There are 1 best solutions below

0
On

It isn't needed. Since so much of our data is handled asynchronously angular already recognizes this and will fail silently in the view when it encounters undefined variables

You could replace what you have with

<div ng-if="galleryPictures.length" >

Checking for length > 0 isn't needed either since zero is falsy