无法调用控制器构造函数并提交函数

时间:2016-04-15 12:00:04

标签: css angularjs controller ng-submit

我有以下代码,我无法找出为什么我的控制器没有被调用? -



index.html -
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1"
    <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
    <!-- build:css(.) styles/vendor.css -->
    <!-- bower:css -->
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
    <!-- endbower -->
    <!-- endbuild -->
    <!-- build:css(.tmp) styles/main.css -->
    <link rel="stylesheet" href="styles/main.css">
    <!-- endbuild -->
  </head>
  <body ng-app="myApp">
    

    <div class="container">
  <div class="row">  
 
      <div class="col-xs-12">
          <!-- FORM ============ -->
        
          <form name="userForm" ng-submit="main.submitForm()" novalidate>

              <!-- USERNAME -->
              <div class="form-group" ng-class="{'has-error' : userForm.username.$invalid && !userForm.username.$pristine }">
                  <label>Username</label>
                  <input type="text" name="username" class="form-control" ng-model="user.username" ng-minlength="3" ng-maxlength="8">
                  <p ng-show="userForm.username.$error.minlength" class="help-block">Username is too short.</p>
                  <p ng-show="userForm.username.$error.maxlength" class="help-block">Username is too long.</p>
              </div>
              
              <!-- Password -->
              <div class="form-group" ng-class="{ 'has-error' : userForm.pwd.$invalid && !userForm.pwd.$pristine }">
                  <label>Password</label>
                  <input type="password" name="password" class="form-control" ng-model="user.pwd">
                  <p ng-show="userForm.pwd.$invalid && !userForm.pwd.$pristine" class="help-block">Enter a valid Password.</p>
              </div>
              
              <button type="submit" class="btn btn-primary col-xs-12" ng-disabled="userForm.$invalid">Submit</button>
              
          </form>

      </div>

  </div>
</div>

    <div class="footer footertxt">
      <div class="container">
        <p>@Copyright xyz</p>
      </div>        </div>


    
    <script type="text/javascript" src="cordova.js"></script>

    <!-- build:js(.) scripts/vendor.js -->
    <!-- bower:js -->
    <script src="bower_components/jquery/dist/jquery.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
    <script src="bower_components/angular-animate/angular-animate.js"></script>
    <script src="bower_components/angular-cookies/angular-cookies.js"></script>
    <script src="bower_components/angular-resource/angular-resource.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
    <script src="bower_components/angular-touch/angular-touch.js"></script>
    <!-- endbower -->
    <!-- endbuild -->

        <!-- build:js({.tmp,app}) scripts/scripts.js -->
        <script src="scripts/app.js"></script>
        <script src="scripts/controllers/main.js"></script>
        <script src="scripts/controllers/about.js"></script>
        <script src="scripts/services/cordova.js"></script>
        <!-- endbuild -->
</body>
</html>
&#13;
&#13;
&#13;

&#13;
&#13;
main.js:

'use strict';


angular.module('myApp')
  .controller('MainCtrl', function ($scope, cordova) {
    console.log("Hello...")
    this.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
    cordova.ready.then(function () {
        alert('Cordova is ready');
    });

    // function to submit the form after all validation has occurred            
    $scope.submitForm = function() {

        // check to make sure the form is completely valid
        if ($scope.userForm.$valid) {
            alert('our form is amazing');
            console.log("For submitted..")
        }

    };
});
&#13;
&#13;
&#13;

app.js:

&#13;
&#13;
/**
 * @ngdoc overview
 * @name angularCordovaTickrv01App
 * @description
 * # myApp
 *
 * Main module of the application.
 */
angular
  .module('myApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl',
        controllerAs: 'main'
      })
      .otherwise({
        redirectTo: '/'
      });
  });
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

修改您的ng-submit,您不必放置main.submitForm()

<form name="userForm" ng-submit="submitForm()" novalidate>

此外,您的控制器出现cordova注射时出错。

查看this Plunker

答案 1 :(得分:0)

我看不到你的index.html中有ng-view的任何地方,你的视图被加载到哪里。放置<div ng-view></div>

答案 2 :(得分:0)

由于您在控制器变量上调用submitForm,因此不应在$scope上设置该函数,而应在控制器上设置:

// $scope.submitForm = function() {
this.submitForm = function() {