1

I am very new with AngularJs and I am trying to submit a form. I used yeoman to generate the angular application. This is the code in the main.html file

<div class="jumbotron">
  <h3>Test</h3>
  <br><br>
  <p class="lead">
    <form name="userForm" ng-submit="loadData()"  ng-controller="myCtrl">
        <!-- NAME -->
        <div class="form-group" ng-class="{ 'has-error' : userForm.name.$invalid && !userForm.name.$pristine }">
            <label>name*</label>
            <input type="text" name="name"  ng-model="name"class="form-control" ng-model="user.name" required>
        </div>
        <br><br>
        <button type="submit" class="btn btn-primary">submit</button> <br><br>
    </form>
  </p>
</div>

and this is the code in the main.js file

angular.module('myApp')
  .controller('MainCtrl', ['$scope', function($scope) 
  {



$scope.loadData = function() {
console.log('submited')
};

}]);

The loadData function never get called. When I added the code to the main.js file I started getting an error in the console, I don't know if it's related to the problem.

Warning: Task "jshint:all" failed. Use --force to continue.

Aborted due to warnings.
3
  • you have linting problem in your code, you code is correct. Just format your code and check for any failed linting rules Commented Feb 20, 2017 at 21:56
  • 1
    Compare your controller name in .html and .js... They are different (myCtrl and MainCtrl) Commented Feb 20, 2017 at 22:29
  • The <input> element is erroneous with two ng-model directives. Commented Feb 21, 2017 at 5:07

1 Answer 1

1
<div ng-app="myApp" ng-controller="MainCtrl" class="jumbotron">
<h3>Test</h3>
<br><br>
<p class="lead">
<form name="userForm" ng-submit="loadData()"  >
    <!-- NAME -->
    <div class="form-group" ng-class="{ 'has-error' : userForm.name.$invalid && !userForm.name.$pristine }">
        <label>name*</label>
        <input type="text" name="name"  ng-model="name"class="form-control" ng-model="user.name" required>
    </div>
    <br><br>
    <button type="submit" class="btn btn-primary">submit</button> <br><br>
   </form>
 </p>
</div>

you forgot to mention the ng-app and the name in controller doesn't match

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.