2015-06-24 42 views
0

我在ng-switch中有一個電子郵件表格。每當我點擊發送控制檯提供一個錯誤。只有當我將ng-switch添加到表單時纔會出現錯誤。不知道爲什麼這是一個jQuery的錯誤。ng開關內的電子郵件表格給出錯誤

控制檯錯誤

TypeError: Cannot read property 'jquery' of undefined 
at Function.n.param (jquery.min.js:4) 
at n.$scope.submit (contactController.js:20) 
at angular.min.js:199 
at f (angular.min.js:216) 
at n.$eval (angular.min.js:126) 
at n.$apply (angular.min.js:126) 
at HTMLFormElement.<anonymous> (angular.min.js:216) 
at HTMLFormElement.n.event.dispatch (jquery.min.js:3) 
at HTMLFormElement.r.handle (jquery.min.js:3) 

contact.html

<div class="container" ng-switch on="contactsent"> 
    <div class="contact col-xs-12" ng-switch-when="default"> 
     <form ng-submit="submit(contactform, $event)" name="contactform" method="post" action="" role="form"> 
      form inputs button etc.. 
     </form> 
    </div> 

    <div class="contact-sent col-xs-12 center" ng-switch-when="success"> 
     <h2 class="contact-sent-message">You message has been succesfully sent!</h2> 
     <a class="contact-sent-back" href="/#/home">Click here to go back</a> 
    </div> 
</div> 

控制器文件

MyApp.controller('contactController', function ($scope, $http) { 

    $scope.contactsent = 'default'; 

    $scope.result = 'hidden' 
    $scope.resultMessage; 
    $scope.formData; //formData is an object holding the name, email, subject, and message 
    $scope.submitButtonDisabled = false; 
    $scope.submitted = false; //used so that form errors are shown only after the form has been submitted 
    $scope.submit = function(contactform, e) { 

     e.preventDefault(); 

     $scope.submitted = true; 
     $scope.submitButtonDisabled = true; 
     if (contactform.$valid) { 
      $http({ 
       method : 'POST', 
       url  : 'contact-form.php', 
       data : $.param($scope.formData), //param method from jQuery 
       headers : { 'Content-Type': 'application/x-www-form-urlencoded' } //set the headers so angular passing info as form data (not request payload) 
      }).success(function(data){ 
       console.log(data); 
       if (data.success) { //success comes from the return json object 
        $scope.submitButtonDisabled = true; 
        $scope.resultMessage = data.message; 
        $scope.result='bg-success'; 
        $scope.contactsent = 'success'; 

       } else { 
        $scope.submitButtonDisabled = false; 
        $scope.resultMessage = data.message; 
        $scope.result='bg-danger'; 
       } 
      }); 
     } else { 
      $scope.submitButtonDisabled = false; 
      $scope.resultMessage = 'Failed <img src="http://www.chaosm.net/blog/wp-includes/images/smilies/icon_sad.gif" alt=":(" class="wp-smiley"> Please fill out all the fields.'; 
      $scope.result='bg-danger'; 
     } 
    } 
}); 
+0

我們可以看到控制器文件? –

+0

剛剛添加的控制器 –

回答

1

編輯:

問題是與這條線,$scope.formData是不確定的,而之前,它必須是一個對象或數組

data : $.param($scope.formData), //param method from jQuery 
+0

啊我明白了。有什麼方法可以使它與ng開關一起工作嗎? –

+0

嘗試也把表格代碼,並會給你正確的一個 –

+0

酷,它現在很多謝謝! –