initializing AngularJS controller member -


i'm trying create html/angularjs form submits data webserver. page loading controller because execute submit function. "referenceerror: formdata not defined" error when reference formdata data. thought proper way initialize members of controller.

var app = angular.module('messagingform', []); app.controller('messagingcontroller', function ($scope, $http) {     $scope.formdata = {                          username: "bob",                          email: "bob@bob.com",                          subject: "why",                          message: "why not?"                      };      $scope.submitted = false; //used form errors shown after form has been submitted     $scope.submit = function(sendcontact) {         $scope.submitted = true;          console.log('validating data');         if (sendcontact.$valid) {             console.log('sending data');             $http({                 method  : 'post',                 url     : 'email.php',                 data : {                     'name': formdata.username,                     'email': formdata.email,                     'subject': formdata.subject,                     'message': formdata.message                 },                 headers : { 'content-type': 'application/x-www-form-urlencoded' }  //set headers angular passing info form data (not request payload)             }).success(function(data){              });         } else {              console.log('validating not good');             alert('failed');         }     } }); 

i'm unclear how initialize member variable guess...what right way this?

matt

try this:

change:

data: {        'name': formdata.username,        'email': formdata.email,        'subject': formdata.subject,        'message': formdata.message     }, 

to

data: {    'name': $scope.formdata.username,    'email': $scope.formdata.email,    'subject': $scope.formdata.subject,    'message': $scope.formdata.message }, 

then:

var app = angular.module('messagingform', []);  app.controller('messagingcontroller', function($scope, $http) {    $scope.formdata = {      username: "bob",      email: "bob@bob.com",      subject: "why",      message: "why not?"    };      $scope.submitted = false; //used form errors shown after form has been submitted    $scope.submit = function(sendcontact) {      $scope.submitted = true;        console.log('validating data');      if (sendcontact.$valid) {        console.log('sending data');        $http({          method: 'post',          url: 'email.php',          data: {            'name': $scope.formdata.username,            'email': $scope.formdata.email,            'subject': $scope.formdata.subject,            'message': $scope.formdata.message          },          headers: {            'content-type': 'application/x-www-form-urlencoded'          } //set headers angular passing info form data (not request payload)        }).success(function(data) {          });      } else {          console.log('validating not good');        alert('failed');      }    }  });

because in code, «formadata» doesn't exist in context. might try declaring local variable, this:

var formdata = {     username: "bob",     email: "bob@bob.com",     subject: "why",     message: "why not?" }; 

example:

var app = angular.module('messagingform', []); app.controller('messagingcontroller', function ($scope, $http) {     var formdata = {         username: "bob",         email: "bob@bob.com",         subject: "why",         message: "why not?"     };      $scope.submitted = false; //used form errors shown after form has been submitted     $scope.submit = function(sendcontact) {         $scope.submitted = true;          console.log('validating data');         if (sendcontact.$valid) {             console.log('sending data');             $http({                 method  : 'post',                 url     : 'email.php',                 data : {                     'name': formdata.username,                     'email': formdata.email,                     'subject': formdata.subject,                     'message': formdata.message                 },                 headers : { 'content-type': 'application/x-www-form-urlencoded' }  //set headers angular passing info form data (not request payload)             }).success(function(data){              });         } else {              console.log('validating not good');             alert('failed');         }     } }); 

Comments