使用Angular 2将对象从搜索推送到数组中

时间:2017-12-21 17:19:58

标签: javascript angular

我创建了这个搜索功能,可以访问API并生成结果。我现在正试图将我找到的结果推送到数组上。如何将特定选择链接到我的ngModel?

我的老方法是手动文字:

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

推送功能:

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

我的新搜索功能:

 <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>

1 个答案:

答案 0 :(得分:1)

new GestureDetector( onTap: () async { print("Container clicked"); Prediction p = await showGooglePlacesAutocomplete( context: context, apiKey: Consts.googlePlacesApiKey, mode: Mode.fullscreen, language: "fr", components: [new Component(Component.country, "fr")]); if (p != null) { (_scaffoldKey.currentState).showSnackBar( new SnackBar(content: new Text(p.description))); } }, child: new TextFormField( // controller: controller, decoration: const InputDecoration( icon: const Icon(Icons.room), hintText: 'Où êtes vous ?', labelText: 'Localisation', ), ), ), 传递到 new GestureDetector( onTap: () async { print("Container clicked"); Prediction p = await showGooglePlacesAutocomplete( context: context, apiKey: Consts.googlePlacesApiKey, mode: Mode.fullscreen, language: "fr", components: [new Component(Component.country, "fr")]); if (p != null) { (_scaffoldKey.currentState).showSnackBar( new SnackBar(content: new Text(p.description))); } }, child: new Container( width: 80.0, height: 80.0, margin: new EdgeInsets.all(10.0), color: Colors.black), ), 事件处理程序

result

并在处理程序中将传递的参数推送到数组

click