0

I want to display a error message when some one forget to enter the group name. So i have created a group.name model for the input tag. Condition is checked when createGroup function is called(on submitting the form). But it is giving "TypeError: Cannot read property 'name' of undefined" while reading name property.

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

Controller

 $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;
         }
  }
4
  • is it occurs every time ? did u try to fill the form and submit ? Commented Feb 10, 2015 at 10:39
  • 3
    Why is ng-model="group" on the form? Commented Feb 10, 2015 at 10:41
  • 1
    The conditions in your if block are in wrong order. Check for group first. Commented Feb 10, 2015 at 10:42
  • You should change order in if (!group || !group.name) { other way around has no sence. Commented Feb 10, 2015 at 10:46

1 Answer 1

1

use angular.isDefined(); to check whether its defined or not in the scope here is the DOC

if ( angular.isDefined(group) && group.hasOwnProperty('name') ) {
    console.log("group", group);
    $scope.emptyName = true;
    $scope.selectServer = false;
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.