angularjsangularjs-forms

AngularJS Form is not working, $scope.submit. undefined error


I have simple angularJS app, which got module, controller, service and directive; along with template html page which holds form variable and structure and print inside main html page index.page using directive which is working fine. Now I have ng-submit="submitForm() in form but I cannot see any data when I press submit; in firefox firebug its says undefined $scope.submitForm.Employee

full detail are as following;

Error: $scope.submitForm.employee is undefined
efController/$scope.submitForm@http://localhost:59662/App/EmployeeForm /efController.js:17:13
anonymous/fn@http://localhost:59662/Scripts/angular.min.js line 213 >    Function:2:218
Ic[b]</<.compile/</</e@http://localhost:59662/Scripts/angular.min.js:254:74
lf/this.$get</r.prototype.$eval@http://localhost:59662/Scripts/angular.min.js:133:309
lf/this.$get</r.prototype.$apply@http://localhost:59662/Scripts/angular.min.js:134:12
Ic[b]</<.compile/</<@http://localhost:59662/Scripts/angular.min.js:254:124
If@http://localhost:59662/Scripts/angular.min.js:35:365
Hf/d@http://localhost:59662/Scripts/angular.min.js:35:314

AngularFormsApp.js (module)

var angularFormsModule = angular.module('angularFormsApp', []);

efController.js (controller)

angularFormsModule.controller('efController',
function efController($scope, efService) {

    $scope.employee = efService.employee;

    $scope.departments = [
         "Engineering",
         "Marketing",
         "Finance",
         "Admin",
         "IT"
    ];

    $scope.submitForm = function () {

       ????????????????????
    }
});

efService.js (service)

angularFormsModule.factory('efService',
function efService() {

    return {
        employee: {
            fullName: "Khurram Zahid",
            notes: "The top employee",
            department: "IT",
            perkCar: true,
            perkStock: false,
            perkSixWeeks: true,
            payrollType: "none"
        }
    }
});

efDirective.js (directive)

angularFormsModule.directive('employeeForm',
function () {

    return {
        restrict: 'E',
        templateUrl: 'App/EmployeeForm/efTemplate.html'
    }
});

efTemplate.html (form)

<form class="form-horizontal" role ="form" ng-submit="submitForm()">

<div class="form-group">
    <label for="fullName" >Name</label>
    <input type="text" id="fullName" name="fullName" class="form-control" ng-model="employee.fullName" />
</div>

<div class="form-group">
    <label for="notes">Notes</label>
    <textarea name="notes" id="notes" class="form-control" rows="5" cols="50" ng-model="employee.notes"></textarea>
</div>

    <div class="form-group">
        <label for="department">Department</label>
        <select name="department" id="department" class="form-control" ng-model="employee.department" ng-options="dept for dept in departments">   </select>
    </div>

    <br />

    <span><b>Perks</b></span>

    <div class="checkbox">
        <label><input type="checkbox" value="perkCar" ng-model="employee.perkCar"/>Company Car</label>
    </div>

    <div class="checkbox">
        <label><input type="checkbox" value="perkStock" ng-model="employee.perkStock"/>Stock</label>
    </div>

    <div class="checkbox">
        <label><input type="checkbox" value="perkSixWeeks" ng-model="employee.perkSixWeeks"/>Six Weeks Leave</label>
    </div>

    <div class="radio-inline">
        <label><input type="radio" name="payrollType" value="W-2" ng-model="employee.payrollType"> W-2</label><br>
        <label><input type="radio" name="payrollType" value="1099" ng-model="employee.payrollType"> 1099</label><br>
        <label><input type="radio" name="payrollType" value="none" ng-model="employee.payrollType"> None</label>
    </div>

<br/><br />

<div class="form-group">
    <input type="submit" class="btn btn-primary" value="Submit" />
</div>

Main HTML Page (index.html)

<!DOCTYPE html>
<html ng-app="angularFormsApp">
<head>
 <title>Angular Form</title>
 <meta charset="utf-8" />
 <link href="Content/bootstrap.min.css" rel="stylesheet" />
  <script src="Scripts/angular.min.js"></script>
  <script src="App/AngularFormsApp.js"></script>
  <script src="App/EmployeeForm/efController.js"></script> 
  <script src="App/EmployeeForm/efDirective.js"></script>
  <script src="App/EmployeeForm/efService.js"></script>

 </head>
 <body class="container">

 <h1>Test AngularJS App Approach</h1>

 <div ng-controller="efController" >

    <employee-form />

</div>  

The above code shows all data correctly that is define in efService for employee


Solution

  • As was mentioned in one of comments, you can NOT get properties of a function itself:

    $scope.submitForm.employee - not going to work, since $scope.submitForm is a function

    What you can do, accessing 'FormController' of your form. You need to give name for your form name="employeeData" and names for each input name="perkStock"

    Then in 'efController' you can access 'employeeData' object, which is 'FormController'. This object has tons of useful properties: $valid, $dirty , etc. + every input by it's name, with their own properties: $viewValue, $modelValue, etc.

    Or you can just use $scope.employee since there is two way binding through ng-model="employee.perkStock"- which we have for each input

    Here is working Plunk with your code.

    From controller, printing our form data:

    $scope.submitForm = function (employeeData) {
    
       console.log('Printing "FormController" object, which is accesible through $scope, or might be passed as submit function parametr.')
       console.log(employeeData); 
       console.log($scope.employeeData);
    
       console.log('Printing employee object, which is binded through ng-model ')
       console.log($scope.employee)
    }