0

I've tried to check if form is pristine:

@ViewChild('myForm') form : ElementRef;

var pristine = this.form.nativeElement.hasClass('ng-pristine')

However, I get error:

this.form.nativeElement.hasClass

So, apparently, the solution from Angular 1 (How to check if element has class with AngularJS?) doesn't work here.

What is the way to do that in Angular 2?

Community
  • 1
  • 1
9ilsdx 9rvj 0lo
  • 6,723
  • 7
  • 26
  • 66
  • 2
    That's the wrong approach. Get the `NgForm` directive and read the `isPristine` property from there. Direct DOM access is discouraged in Angular2. The forms docs at angular.io (validation) should demonstrate how to do it – Günter Zöchbauer Nov 10 '16 at 12:47
  • @GünterZöchbauer and how do I create binding using NgForm? Because google search has given me angular 1 code once again, that uses $scope: http://stackoverflow.com/questions/22151577/check-pristine-status-of-ngmodel-without-using-a-form – 9ilsdx 9rvj 0lo Nov 10 '16 at 12:59
  • https://angular.io/docs/ts/latest/cookbook/form-validation.html – Günter Zöchbauer Nov 10 '16 at 13:00

0 Answers0