hussain
hussain

Reputation: 7113

How to check ng-model variable value undefined using ng-change?

if value is not selected for ng-model selectedFileSize its throwing error TypeError: Cannot read property 'size' of undefined when i click on button startRecording. How can i resolve this problem ?

main.html

<div class="col-md-3">
    <select class="form-control" ng-model="selectedFileSize" ng-options="item as item.value for item in FileSizeOptions" ng-change="onSizeChange()"><option value="">Select</option></select>
</div>
<div class="col-md-2">
    <button type="button" class="btn btn-primary" ng-click="startRecording()">Start Recording</button>
</div>

ctrl.js

 if (($scope.selectedFileSize.size !== "") || ($scope.selectedFileSize !== undefined)) {
    //logic  here 

}

Upvotes: 2

Views: 748

Answers (1)

dfsq
dfsq

Reputation: 193291

You need to check that selectedFileSize is populated before checking selectedFileSize.size:

if ($scope.selectedFileSize && $scope.selectedFileSize.size) {
    //logic  here 
}

Upvotes: 3

Related Questions