[英]ERROR TypeError: Cannot read property 'name' of undefined - Using angularjs
[英]TypeError: Cannot read property 'name' of undefined in angularJS
當有人忘記輸入組名時,我想顯示一條錯誤消息。 所以我為輸入標簽創建了一個group.name模型。 調用createGroup函數時(提交表單時)檢查條件。 但是它在讀取名稱屬性時給出“ TypeError:無法讀取未定義的屬性'名稱' ”。
<form name="serverGroup" role="form" ng-model="group" ng-submit="createGroup(group)">
<div class="form-group createGrpField">
<label for="groupname" class="nameField">Name</label>
<input id="nameField" type="text" ng-model="group.name" class="form-control" placeholder="Enter group name" />
</div>
<div class="createServerPadding">
<button id="saveBtn" class="btn btn-default btn-primary" type="submit">Save</button>
<button class="btn btn-default" type="cancel">Cancel</button>
</div>
</form>
調節器
$scope.createGroup = function(group) {
selectedRows = $scope.gridApi.selection.getSelectedRows();
$scope.emptyName = false
$scope.selectServer = false;
if (!group.name || !group) {
console.log("group", group);
$scope.emptyName = true;
$scope.selectServer = false;
}
}
使用angular.isDefined();
檢查其范圍是否在這里定義的DOC
if ( angular.isDefined(group) && group.hasOwnProperty('name') ) {
console.log("group", group);
$scope.emptyName = true;
$scope.selectServer = false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.