There are two components. On one of the components is an input group. Which by clicking on the button opens a modal window with a list of positions.
<div class="form-group form-black">
<label class="control-label">Position<label style="color:red;">*</label></label>
<div class="input-group">
<input type="text" class="form-control" id="positions_id" #positions_id='ngModel' name="positions_id" [(ngModel)]="user.positions_id" required>
<span type="submit" class="input-group-addon btn btn-info" (click)="onOpenModule();">Change position</span>
</div>
</div>
And on the second component is the modal window itself. So when I open this modal window and select the position and then click on the "Apply" button, this post should entered in the input. Here's how to implement the function for the "Apply" button?
html:
<div class="col-md-12">
<table class="table table-hover">
<thead>
<tr >
<th ><b>Position</b></th>
</tr>
</thead>
<tbody>
<tr class="{{selectedPosition == position ? 'active' : ''}}" *ngFor="let position of positions" (click)="updateSelectedPosition(position?.position_id)">
<td>{{position.name}} </td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-12">
<button type="submit" [disabled]="!selectedPositon?.name" class="btn btn-info pull-right">Apply</button>
</div>
ts:
import { Component, OnInit } from '@angular/core';
import { DialogRef, ModalComponent } from 'angular2-modal';
import { BSModalContext } from 'angular2-modal/plugins/bootstrap';
import { PositionService} from '../../../../services/position/position.service';
import { Position } from '../../../../models/position.model';
import { AuthService } from '../../../../services/auth/auth.service';
import { Observable } from 'rxjs/Rx';
@Component({
selector: 'app-position-modal',
templateUrl: './position-modal.component.html',
styleUrls: ['./position-modal.component.scss'],
})
export class PositionModalComponent implements ModalComponent<any> {
positions: Array<Position>;
selectedPosition = null;
constructor(
public dialog: DialogRef<any>,
public authService: AuthService,
private servPosition: PositionService
) {
this.positions = new Array<Position>();
}
ngOnInit() {
this.loadPositions();
}
private loadPositions() {
this.servPosition.getPositions().subscribe(positions => this.positions = positions);
}
updateSelectedPosition(PositionId) {
this.selectedPosition = this.positions.find(el => {
return el.position_id === PositionId
})
}
}
@output. If you don't know about services or@output, check the official docs and tutorials. - Fussel