Angular 5 property binding from within the component html for the same component

Ahmed Elkoussy

I am using the property isPersonalInfoValid as a flag for a wizard component

I want the emailform.valid (emailForm is just a reference to the ngForm shown in the html below) to update the component property [isPersonalInfoValid], I tried many ways but none worked, currently this [(isPersonalInfoValid)] is treated as a property of the emailForm which is wrong, I want to use it as a property of personal-info component.

The code below is from the personal-info.component.html

<form #emailForm="ngForm" [(isPersonalInfoValid)]="emailForm.valid">
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" name="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email"
      [(ngModel)]="data.email" required email>

The code of personal-info.component.ts is as follows:

import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { FormWizardModule } from 'angular2-wizard';

@Component({
  selector: 'app-personal-info',
  templateUrl: './personal-info.component.html',
  styleUrls: ['./personal-info.component.css'],
  // encapsulation: ViewEncapsulation.None
})
export class PersonalInfoComponent implements OnInit {

  @Input() isPersonalInfoValid = false;
  data: any = {
    email: '[email protected]'
  };
  constructor() { }

  ngOnInit() {
  }

  onStep1Next(event) {
    console.log('Step1 - Next');
  }

  onStepChanged(step) {
    console.log('Changed to ' + step.title);
}

}

Your help is appreciated, thanks

Edit:

I can bind the property only if I am using the component selector (in another component) but then I can't access the form.valid property , like this

<app-personal-info #personalInfo  [isPersonalInfoValid]="emailForm.valid ></app-personal-info>

I need to make the binding so that the form.valid changes the property isPersonalInfoValid

Edit 2:

The code is here

Yerkon

No need to create another variable to check validity of the form. Get access to form where you need. In your case parent need to get access of form in the child component. It can be solved in two ways by official documentations:

  1. Little More Code. You can access to child components by injecting them into the parent as a ViewChild. Read more: Parent calls an @ViewChild()
  2. Simple and Easy way. If you want to access child only in parent component template, use local variables(#var). Read more: Parent interacts with child via local variable

For you case I already @ViewChild approach. It gives you full access to child component, but also little more code than second approach using local variables.

ViewChild Decorator. Official doc.

Get access to child PersonalInfoComponent component in parent WizardComponent:

 ...
 @ViewChild(PersonalInfoComponent)
  private personalInfo: PersonalInfoComponent;
 ...

In child PersonalInfoComponent get access to emailForm similarly as above:

@ViewChild('emailForm') emailForm: NgForm 

Then, in parent WizardComponent you can get emailForm throw PersonalInfoComponent child component. And pass it to wizard-step component as this personalInfo.emailForm.valid

WizardComponent.html:

  ...
  <wizard-step [title]="'Personal Information'" [isValid]="personalInfo.emailForm.valid" (onNext)="onStep1Next($event)">
          <app-personal-info #personalInfo></app-personal-info>

  </wizard-step>
  ...

StackBlitz EXAMPLE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Update property binding from within a component

Angular5 - Change parent's property from child component's input's [(ngModel)] binding

Angular 2 / typescript binding text to <meta property="og"> from component to index.html

How to pass data to the input in angular html component using property binding

Change a property of a component from another component and render it in the html in angular 2

Angular 2 : multiple HTML pages within same component

binding the data from component in HTML page : Angular 6

Event Binding from within a Blazor Component

Binding to a component property in angular2

Inter component property binding in Angular 2

HTML5 audio component from within React

Angular5 - Same variable in html and in component is different

Detecting changes to a model within the same component - Angular

binding data to a nonnative html property within Angular

Binding a Component property in Vue

Angular property binding target can change component property

Can an angular component be identified within an HTML element?

Angular 2: Binding to Functions from Component Templates

Binding properties from service to component Angular 4

Angular 2 Parent Component Losing Input Binding from Child Component

Accessing `selector` from within an Angular 2 component

Angular - Component Binding Issue

Question on .this binding within a React Component

Different Highcharts in same component (Angular 5)

extjs 5 : make a data binding for component's custom property

Angular - Is binding a component method to DOM target property a wrong practice?

Angular 2: Parent-Child Component Property Binding

Binding to child component's property in Angular version 8.1.2

Can we overwrite existing html component with same name component in Angular?