CelsomTrindade CelsomTrindade - 1 year ago 148
AngularJS Question

AngularJS form Cannot read property '$setPristine' of undefined

I know there is other similar questions to this, but i've read them all and none of them is working for me.

When i try to set my form to a pristine, I keep getting this error:

TypeError: Cannot read property '$setPristine' of undefined

The controller and my angular version (1.4.2) are everything ok, because i also have other thing happening within the same function calling the
method and that one is working.

This is the code i'm using:


<form name="cadTel" novalidate>
<div class="form_group">
<label class="col-md-4 f--label"><i class="fa fa-asterisk"></i>Nome</label>
<div class="col-md-8 f--input">
<input type="text" name="ds_contato" ng-model="tel.ds_contato" ng-required="true" />
<div class="form_group">
<label class="col-md-4 f--label"><i class="fa fa-asterisk"></i>Telefone</label>
<div class="col-md-8 f--input">
<input type="text" name="num_tel" mask="(99) 9?9999-9999" ng-model="tel.num_tel" ng-required="true" />
<input type="button" class="bt-m bt-suc" name="cadastrar" value="Salvar" ng-click="add_telefone(tel)">
<div class="bt-m bt-war" ng-click="reset()">Limpar</div>


$scope.tel = {};

$scope.add_telefone = function(tel) {
$scope.tel = angular.copy(tel);
method: 'POST',
.success(function (data, status, headers, config) {
.error(function (data, status, headers, config) {
$scope.reset = function() {
$scope.tel = {};

The option to clean the values are working but to set pristine none.

Any ideas?

Answer Source

I faced the same issue and below fix corrected it. Angular is not aware of the form id. Please change the form name as below

form name="form.cadTel"

Also during the controller startup set the form

$scope.form = {};

Check this plnkr [link]http://plnkr.co/edit/C88Pnr4xIn11vqDkJXRZ?p=preview

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