sarunast sarunast - 1 year ago 129
AngularJS Question

AngularJs can't access form object in controller ($scope)

I am using bootstrap-ui more specifically modal windows. And I have a form in a modal, what I want is to instantiate form validation object. So basically I am doing this:

<form name="form">
<div class="form-group">
<label for="answer_rows">Answer rows:</label>
<textarea name="answer_rows" ng-model="question.answer_rows"></textarea>

{{form | json}}

I can see form object in the html file without no problem, however if I want to access the form validation object from controller. It just outputs me empty object. Here is controller example:

.controller('EditQuestionCtrl', function ($scope, $modalInstance) {
$scope.question = {};
$scope.form = {};

$scope.update = function () {
console.log($scope.form); //empty object
console.log($scope.question); // can see form input

What might be the reasons that I can't access
from controller ?

Answer Source

Just for those who are not using $scope, but rather this, in their controller, you'll have to add the controller alias preceding the name of the form. For example:

<div ng-controller="ClientsController as clients">
  <form name="clients.something">

and then on the controller:

app.controller('ClientsController', function() {
  // setting $setPristine()

Hope it also contributes to the overall set of answers.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download