简体   繁体   English

使用Angular 2将对象从搜索推入数组

[英]Push object from search using Angular 2 into array

I created this search function that hits an API and generates results. 我创建了这个搜索功能,该功能可以访问API并生成结果。 I am now trying to push my found result on click to an array. 我现在正尝试将我发现的结果推到数组上。 How do I link a particular selection to my ngModel? 如何将特定选择链接到ngModel?

My old way was manual text: 我以前的方式是手工文本:

<input type="text" class="form-control" placeholder="Angular" [(ngModel)]="company.technology[i].stack" name="technology_{{i}}" #technology="ngModel" required>

function to push: 推送功能:

onAddStack() {
   this.company.technology.push({stack: ''});
}

My new search feature: 我的新搜索功能:

 <input (keyup)="$event.target.value && searchTerm$.next($event.target.value)">
    <ul *ngIf="results">
     <a *ngFor="let result of results | slice:0:7" class="other" (click)="onAddStack()">
    <li>{{ result.name }}</li>
    </a>
 </ul>

Pass the result into the click event handler result传递到click事件处理程序中

<a *ngFor="let result of results | slice:0:7" class="other" (click)="onAddStack(result)">
   <li>{{ result.name }}</li>
</a>

and in the handler push the passed parameter into the array 并在处理程序中将传递的参数推入数组

onAddStack(result) {
   this.company.technology.push(result);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM