Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angularjs

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>     </div> </form>  <pre>     {{form | json}} </pre 

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 $scope.form from controller ?

like image 244
sarunast Avatar asked Feb 05 '14 10:02

sarunast


1 Answers

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">   </form> </div> 

and then on the controller:

app.controller('ClientsController', function() {   // setting $setPristine()   this.something.$setPristine(); }; 

Hope it also contributes to the overall set of answers.

like image 147
Jorge Avatar answered Sep 20 '22 18:09

Jorge