Issue
I am using a modal to get a title from the user. Save button is used to trigger Save() method which get the users title and closes modal. I want to use this title later on another method called for a different event. Is any possible way to keep this value and use it later, because since this value is saved to Save() method is a local variable. I am using Angular
<ng-template #template>
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title col-md-10">Add topics</h4>
<button type="button col-md-2" class="close pull-right" aria-label="close" (click)="closeModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="form-group col-md-8 offset-2">
<h5 class="text-center" style="margin-top: 2rem;">Enter Title</h5>
<input [(ngModel)]="topicTitle" type="text" class="form-control" id="ChatTitle" placeholder="Enter Title">
</div>
<div class="modal-body"><hr />
<h5 class="text-center">Select tags that match you theme</h5>
<ng-select [items]="tags"
bindLabel="name"
[multiple]="true"
placeholder="Choose tags"
[(ngModel)]="myTags"
(change)="selectedTags($event)"
>
</ng-select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal" (click)="closeModal()">Cancel</button>
<button type="button" class="btn btn-success" (click)="onSave()">Save</button>
</div>
</div>
Also onSaved() method which belongs to this component
topictitle = '';
onSave() {
console.log(this.topicTitle);
const btn = document.getElementById("addtopic");
this.modalService.hide(1);
btn.innerHTML = "End topic";
btn.setAttribute('class', 'btn btn-danger');
console.log(title);
// New document creation
this.savedService.createSavedDoc(this.topicTitle, this.myTags);
}
An example someExample() method will be called many times but i want to have the title value we got from onSave().
Solution
I think you had a typo or something in topicTitle
in your html and typescript.
Here is a small working example.
When you need the title in the same component:
public title = '';
// please pass the title on method call from the template
public save(newTitle) {
this.title = newTitle;
}
Or you need it in another component, just create a service to handle the title.
Answered By - brandt.codes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.