如果未为ng-model selectedFileSize选择值,则当我单击按钮TypeError: Cannot read property 'size' of undefined时会抛出错误startRecording。我该如何解决这个问题?

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

}

最佳答案

在检查selectedFileSize之前,您需要检查是否已填充selectedFileSize.size

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

关于javascript - 如何使用ng-change检查ng-model变量值未定义?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39043661/

10-13 06:32