Miguel Moura
Miguel Moura

Reputation: 39514

Submit form with angular

I have the following Angular code

controller:

app.controller('MainCtrl', function($scope) {  
  var vm = this;  
  vm.job = null;  
  vm.create = function (job) {      
    vm.job = job;    
  }  
});

HTML:

<div ng-controller="MainCtrl as vm">

  <span data-ng-bind="vm.job.position"></span>

  <form name="form" data-ng-submit="vm.create(vm.job)">

    <label for="position">Position</label>
    <input id="position" name="vm.job.position" type="text" data-ng-model="vm.job.position" />

    <button>Create</button>      

  </form>      

</div>

But when I submit the form I don't see the Position value.

Any idea why?

Upvotes: 0

Views: 286

Answers (1)

JB Nizet
JB Nizet

Reputation: 692221

Because

  1. You forgot to add ng-app to the body or html element
  2. You're using angular 1.0.8, which is completely obsolete, and doesn't support controller as.

Note that you don't even need to submit, since the job you're binding is already vm.job. Your create(vm.job) method call does nothing: it assigns vm.job to vm.job.

Upvotes: 6

Related Questions