hussain hussain - 11 months ago 50
Javascript Question

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

if value is not selected for ng-model

its throwing error
TypeError: Cannot read property 'size' of undefined
when i click on button
. How can i resolve this problem ?


<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 class="col-md-2">
<button type="button" class="btn btn-primary" ng-click="startRecording()">Start Recording</button>


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


Answer Source

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

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