hussain hussain - 3 months ago 11
Javascript Question

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

}

Answer

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

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