3

I'm havings some problems with ng-show and $pristine.

Here is the code (also on CodePen):

<blockquote ng-show="!comment.author.$pristine && !comment.rating.$pristine && !comment.comment.$pristine">
    <p>{{comment.rating}} Stars</p>
    <p>{{comment.comment}}</p>
    <footer>{{comment.author}}
</blockqoute>

When every field on the form has been filled, I want to show the div that contains my duplicate, but I want it to be hidden while some are still empty.

I tried to to use

!comment.[index].$pristine && ....

so when every field has been filled, the blockquote would become visible, but it didn't work.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Lyriene
  • 81
  • 1
  • 11

5 Answers5

2

Hey the way you are going the main problem will be that when the user will be filling any random data in the last text box, the moment he fills a letter the div will be visible to him - despite whatever improvements you make to the code.

What I'll suggest is - make use of ng-show="whatever" for that section that you want to show after the data has been filled.

In the beginning where your controller starts make it false $scope.whatever = false; and now it wont be visible to the user; when the user has filled all the text boxes call a trigger and check if the data is valid or not and then $scope.whatever=true; - Now your section will be visible.

To call the trigger you can do various things - you can make use of ng-change on the last textbox and there check values of all textboxes using their specific model name, I hope you know that.

Let me know if you want further clarification on this.

Gandalf the White
  • 2,415
  • 2
  • 18
  • 39
1

I believe you can specify ng-hide as a className to hide it by default.

<blockquote ng-show="whatever" class="ng-hide"
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
1

Change this

<blockquote ng-show="!comment.author.$pristine && !comment.rating.$pristine && !comment.comment.$pristine">
                          <p>{{comment.rating}} Stars</p>
                          <p>{{comment.comment}}</p>
                          <footer>{{comment.author}}
                    </blockqoute>

To this

<blockquote ng-show="!commentForm.author.$pristine && !commentForm.comment.$pristine">
                              <p>{{comment.rating}} Stars</p>
                              <p>{{comment.comment}}</p>
                              <footer>{{comment.author}}, {{comment.date | date}}</footer>
                        </blockqoute>

Notice I'm using the form to check for the form properties, not the scope properties. Just change comment to commentForm (which is your form's name).

Andrew Myers
  • 2,754
  • 5
  • 32
  • 40
Ariel
  • 1,507
  • 2
  • 20
  • 28
  • @GáborCsikós it's working in the codepen I made. Check that you set a form name and use that name in the ng-show, or make a codepen to see what could be wrong. – Ariel Sep 27 '16 at 15:26
0

I would bind values of the form input to some variables in the controller, and on their ng-change="controller.validate()" run a validate code, so you can check if fields are not empty and input is correct and after that set isBlockquoteVisible variable, that would be in <blockquote ng-show="{{controller.isBlockquoteVisible}}" ...

Ivan Leonenko
  • 2,363
  • 2
  • 27
  • 37
0
<blockquote ng-hide="comment.author.$pristine && comment.rating.$pristine && comment.comment.$pristine">
   <p ng-show="!comment.rating.$pristine">{{comment.rating}} Stars</p>
   <p ng-show="!comment.comment.$pristine">{{comment.comment}}</p>
   <footer ng-show="!comment.author.$pristine">{{comment.author}}</footer>
</blockquote>
Pablo Darde
  • 5,844
  • 10
  • 37
  • 55