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

时间:2016-08-19 16:19:37

标签: javascript angularjs

如果我点击按钮selectedFileSize时没有为ng-model 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 

}

1 个答案:

答案 0 :(得分:2)

在检查a,b,c,d,e a,b,c,d,e a,b,c,d,e a,b,c,d,e f g a,b,c,d,e

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