TypeError:无法读取属性' name'在angularJS中未定义

时间:2015-02-10 10:36:31

标签: javascript angularjs

我想在某人忘记输入群组名称时显示错误消息。所以我为输入标签创建了一个group.name模型。调用createGroup函数时检查条件(提交表单时)。但它正在给予  " TypeError:无法读取属性' name'未定义的"在阅读姓名财产时。

          <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;
         }
  }

1 个答案:

答案 0 :(得分:1)

使用angular.isDefined();检查此处范围内的定义与否是DOC

if ( angular.isDefined(group) && group.hasOwnProperty('name') ) {
    console.log("group", group);
    $scope.emptyName = true;
    $scope.selectServer = false;
 }