2

I've a Form for password change and the "Save password" button is disabled as long as the new password and it's repetition is not equal. To keep the controller 'clean' I've mapped a Template Element Reference to the repetition input.

 <form>
     <input [(ngModel)]="newPassword"
            type="password"
            name="new-password"
            id="new-password">

     <input type="password"
            name="new-password-repeat"
            id="new-password-repeat"
            #passwordRepeatInput>

     <!-- This is the output -->
     <pre>{{passwordRepeatInput.value}}</pre>

    <button [disabled]="!!newPassword && passwordRepeatInput.value"
             class="btn btn-primary">
  Save password
</button>

Now unexpected things happen. When I change the value of the repetition field the output won't change. But once I change another input within the form the output becomes the value of the input element — So it does not behalf like an element having the [(ngModel)] attribute assigned.

Once I specify a new model property in my controller and map it to the repetition field via [(ngModel)] the Template Element Reference is working and changes the output whenever the input value changes.

 <input type="password"
        name="new-password-repeat"
        id="new-password-repeat"
        [(ngModel)]="passwordRepeatModelVal" // This solves the problem
        #passwordRepeatInput>

But Is there a way to establish the expected behavior without having unnecessary properties in the controller?

5
  • I don't understand. What's the difference between calling the component property you're binding to newPasswordRepeat or passwordRepeatModelVal? What's newPasswordRepeat then, in the first case? Commented May 3, 2018 at 15:31
  • For passwordRepeatModelVal I have to decare a property in the controller but using a template reference #passwordRepeatInput there shouldn't be a need for this declaration Commented May 3, 2018 at 15:33
  • But in your first case, newPasswordRepeat is not a template reference, is it? What does it reference? Commented May 3, 2018 at 15:40
  • If newPasswordRepeat is not defined, then passwordRepeatInput.value is always the same (probably an empty string). Commented May 3, 2018 at 15:42
  • The # char and yes he model assignment is too much here Commented May 3, 2018 at 15:42

1 Answer 1

2

The view should also update correctly if you apply the ngModel directive by itself to the repeat input element, without binding it to a property:

<input type="password"
       name="new-password-repeat"
       id="new-password-repeat"
       ngModel
       #passwordRepeatInput>

See this stackblitz.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.