Join the Stack Overflow Community
Stack Overflow is a community of 6.2 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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;
         }
  }
share|improve this question
    
is it occurs every time ? did u try to fill the form and submit ? – K.Toress Feb 10 '15 at 10:39
3  
Why is ng-model="group" on the form? – Wayne Ellery Feb 10 '15 at 10:41
1  
The conditions in your if block are in wrong order. Check for group first. – Donald Supertramp Feb 10 '15 at 10:42
    
You should change order in if (!group || !group.name) { other way around has no sence. – Beri Feb 10 '15 at 10:46

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;
 }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.