0

I cannot get at a form value through $scope.form. In the view and Batarang I see that the form object has the right name for all fields, but no values. On the other hand, the value in the actual form field is correct, as is the $scope.mymodel.headline value (see example below).

Why would this happen?

Sample for 1 field follows. Batarang output:

{ 
  mymodel:  { 
    headline: My value
  } 

  form:  { 
    mymodel[headline]:  {  } 
  } 
}

If I output {{form}} in the view it shows:

mymodel[headline]:  {  } 

Controller code is very simple:

@mymodelCtrl = ['$scope', ($scope) ->
  $scope.init = (mymodel) ->
    $scope.mymodel = mymodel
]

Much simplified HTML:

<div id="new_mymodel" ng-controller="mymodelCtrl" 
  ng-init="init({'headline': 'my value'})" class="ng-scope">
  <form action="/myurl" id="new_mymodel" method="post" name="form" role="form">
    <input id="mymodel_headline" name="mymodel[headline]" 
      ng-model="mymodel.headline" ng-required="true" type="text">
    <input type="submit">
  </form>
</div>

UPDATE: At first I thought the accepted answer was wrong, but that was because of a side issue. But it is correct: the issue is that developer console and batarang and view were displaying something wrong, not that the field is empty. I needed to use $scope.form["mymodel[headline]"].$formVaue.

iftheshoefritz
  • 5,829
  • 2
  • 34
  • 41

1 Answers1

1

You can get it's value from

$scope.form['mymodel[headline]'].$modelValue

See this.

gorpacrate
  • 5,109
  • 3
  • 21
  • 18
  • Isn't your form inside ui-bootstrap modal? There are some issues with initializing forms inside it. If not, could you provide a fiddle? – gorpacrate Jun 20 '14 at 08:37
  • Ok, i've been in that trouble. :) The simplest way is to reinitialize the form after template is rendered. In controller: `$scope.setForm = function(form){$scope.yourFormName = form;};` and at the bottom of modal's template, after : `{{setForm(yourFormName)}}` – gorpacrate Jun 20 '14 at 08:42
  • You're absolutely right, my mistake. The issue is that angular doesn't stringify the field objects nicely, so it comes out blank in batarang and prints "undefined" in console. Using $modelValue in the view or controller works perfectly. My statements in the directive also thought it was undefined, but that's for a different reason. – iftheshoefritz Jun 20 '14 at 11:46