繁体   English   中英

TypeError:无法读取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.

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