AngularJs 错误:参数'FormController'不是函数



好吧,我喜欢2个Angular应用。一个是自动的,另一个是自动的。

现在一切都很好,但是在控制台我有一个错误:

错误:参数'formController'不是一个函数,而是未定义 在错误(本地) 在CB( 等...

但是我已经测试过的表格工作,错误仍然存在。现在破裂的是我想将表格放在ng-viewdiv上方,但是当我这样做时,ng-viewdiv停止工作。

当我从表单中删除ng-controller =" formController"时,将出现ng-viewdiv页面,但表格显然会失去其功能。

也许更有经验的人可以发现问题或提供可能的替代方案。

代码的条。

<html ng-app="kaidoweb">
<head>
    <!-- add javascripts -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="js/angular.min.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script type="text/javascript">
  // creating Angular Module
  var websiteApp = angular.module('websiteApp', []);
  // create angular controller and pass in $scope and $http
  websiteApp.controller('FormController',function($scope, $http) {
  // creating a blank object to hold our form information.
  //$scope will allow this to pass between controller and view
  $scope.formData = {};
  // submission message doesn't show when page loads
  $scope.submission = false;
  // Updated code thanks to Yotam
  var param = function(data) {
        var returnString = '';
        for (d in data){
            if (data.hasOwnProperty(d))
               returnString += d + '=' + data[d] + '&';
        }
        // Remove last ampersand and return
        return returnString.slice( 0, returnString.length - 1 );
  };
  $scope.submitForm = function() {
    $http({
    method : 'POST',
    url : 'process.php',
    data : param($scope.formData), // pass in data as strings
    headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
  })
    .success(function(data) {
      if (!data.success) {
       // if not successful, bind errors to error variables
       $scope.errorName = data.errors.name;
       $scope.errorEmail = data.errors.email;
       $scope.errorTextarea = data.errors.message;
       $scope.submissionMessage = data.messageError;
       $scope.submission = true; //shows the error message
      } else {
      // if successful, bind success message to message
       $scope.submissionMessage = data.messageSuccess;
       $scope.formData = {}; // form fields are emptied with this line
       $scope.submission = true; //shows the success message
      }
     });
   };
});
 angular.element(document).ready(function() {
      angular.bootstrap(document.getElementById('websiteApp'), ['websiteApp']);
    });   
    </script>
</head>
<body>
    <div style="position:relative">
        <div ng-view></div>
    </div>
    <div id="websiteApp" class="contactRow">
        <img class="close" src="img/close.png" ng-click="closeForm()" />
        <form ng-submit="submitForm()" ng-controller="FormController" novalidate class="contactForm" name="form" ng-hide="loaded">
          <input class="input" type="text" name="name" placeholder="SINU NIMI" ng-model="formData.name" ng-class="{'error' : errorName}">
          <input class="input2" type="email" name="email"  placeholder="SINU E-MAIL" ng-model="formData.email" ng-class="{'error' : errorEmail}">
          <div ng-class="{'submissionMessage' : submission}" ng-bind="submissionMessage"></div>
        </form>
    </div>
</body>
</html>

编辑!按需要的额外代码!app.js

    'use strict';
// angular.js main app initialization
var app = angular.module('kaidoweb', []).
    config(['$routeProvider', function ($routeProvider) {
      $routeProvider.
        when('/', {
         templateUrl: 'pages/index.html',
         activetab: 'index',
         controller: HomeCtrl 
       }).
        when('/works', {
          templateUrl: 'pages/works.html',
          controller: PrivacyCtrl,
          activetab: 'works'
        }).
        otherwise({ redirectTo: '/' });
    }]).run(['$rootScope', '$http', '$browser', '$timeout', "$route", function ($scope, $http, $browser, $timeout, $route) {
        $scope.$on("$routeChangeSuccess", function (scope, next, current) {
          $scope.part = $route.current.activetab;
        });
  }]);
app.config(['$locationProvider', function($location) {
    $location.hashPrefix('!');
}]);

和Controller.js

    'use strict';
// optional controllers
function HomeCtrl($scope, $http) {
}
function ProjectCtrl($scope, $http) {
}
function PrivacyCtrl($scope, $http, $timeout) {
}
function AboutCtrl($scope, $http, $timeout) {
}

为什么您需要该表单的第二个Angular应用?将FormController放入您的" Kaidoweb" -App?

是否会更好
var app = angular.module('kaidoweb', []);
//your code goes here
app.config(['$locationProvider', function($location) {
    $location.hashPrefix('!');
}]);
app.controller('FormCrl', ['$scope', '...' function($scope, ...){
    // FormController code goes here
}]);

然后在您的HTML

<body>
<div style="position:relative">
    <div ng-view></div>
</div>
<div class="contactRow" ng-controller="FormCtrl">
    <img class="close" src="img/close.png" ng-click="closeForm()" />
    <form ng-submit="submitForm()" ng-controller="FormController" novalidate class="contactForm" name="form" ng-hide="loaded">
      <input class="input" type="text" name="name" placeholder="SINU NIMI" ng-model="formData.name" ng-class="{'error' : errorName}">
      <input class="input2" type="email" name="email"  placeholder="SINU E-MAIL" ng-model="formData.email" ng-class="{'error' : errorEmail}">
      <div ng-class="{'submissionMessage' : submission}" ng-bind="submissionMessage"></div>
    </form>
</div>
</body>

感谢您的额外代码,我相信您需要在应用程序上声明您的控制器:

 app.controller('yourCtrl'.....

最新更新