简体   繁体   English

如何使用ng-change检查ng-model变量值未定义?

[英]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 . 如果未为ng-model selectedFileSize值,则其抛出错误TypeError: Cannot read property 'size' of undefined当我单击按钮startRecording时, TypeError: Cannot read property 'size' of undefined How can i resolve this problem ? 我该如何解决这个问题?

main.html 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 ctrl.js

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

}

You need to check that selectedFileSize is populated before checking selectedFileSize.size : 您需要检查selectedFileSize在检查之前填充selectedFileSize.size

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM