是否有可能,如果我们从方法调用的输入字段中得到一个值,以便稍后在Angular中调用的另一种方法上使用该值

问题描述 投票:0回答:1

我正在使用模式从用户那里获得标题。保存按钮用于触发Save()方法,该方法获取用户标题并关闭模式。我想稍后在另一个调用另一个事件的方法上使用此标题。有什么可能的方法可以保留该值并在以后使用,因为自从此值保存到Save()方法以来,它是局部变量。我正在使用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">&times;</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>

也属于此组件的onSaved()方法

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);
}

一个示例someExample()方法将被调用很多次,但是我想拥有我们从onSave()获得的标题值。

javascript angular
1个回答
0
投票

可能:-)

当您在同一组件中需要标题时:

public title = '';

// please pass the title on method call from the template
public save(newTitle) {
    this.title = newTitle;
}

或者您在其他组件中需要它,只需创建一个服务即可处理标题。

有关更多详细信息,我们需要您提供更多代码。请查看at how to ask并调整您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.