onselect时将选择的下拉菜单项发送到视图

时间:2018-07-07 11:14:50

标签: django drop-down-menu django-templates

我想通过import { Component, OnInit, OnDestroy } from '@angular/core'; import { Subject, interval } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit, OnDestroy { fruit: string; fruitList: Array<string> = ['apple', 'orange', 'banana']; ngUnsubscribe: Subject<any> = new Subject(); constructor() { } ngOnInit() { // init the first fruit this.fruit = this.fruitList[0]; // create an interval that is going to fire every 5s interval(5000) // unsubscribe from interval when the component is destroyed, averting any memory leak .pipe(takeUntil(this.ngUnsubscribe)) // subscribe to interval .subscribe(() => { // find the current fruit index in the list const fruitIndex = this.fruitList.findIndex((fruit: string) => fruit === this.fruit); // get the next fruit from the list const nextFruit = this.fruitList[fruitIndex + 1]; // if next fruit is defined set displayed fruit with it // else if next fruit is undefined that means you reached the end of the list // so set the displayed fruit with the first list item this.fruit = nextFruit ? nextFruit : this.fruitList[0]; }); } ngOnDestroy() { this.ngUnsubscribe.next(); this.ngUnsubscribe.complete(); } } onselect选项而不是单击按钮提交时将所选下拉项的值发送到我的视图。

我的目标是立即根据所选数据过滤模型对象列表。

我找不到明确的答案,对此我感到困惑。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您应该使用AJAX解决问题

  1. 用户从下拉菜单中选择项目X
  2. 您使用data=X发送后台请求
  3. 您的站点中有一个新的终结点(可能是API?),它处理X并返回响应(响应可以是json或类似的东西)
  4. 用户从步骤2中获得响应,现在从响应数据中更新您的信息(如果需要)

选中this链接以获取有关如何在djnago中使用ajax的一般想法