【问题标题】:How to make a select option as default selected in angular using ng-multiselect-dropdown?如何使用 ng-multiselect-dropdown 将选择选项设置为默认角度?
【发布时间】:2020-03-26 22:50:52
【问题描述】:

所以我在这里从数据库中获取数据并将其存储到事件数组中。此事件数组数据使用 ng-multiselect-dropdown 显示在 book-ticket.component.html 上。从这里选择一个选项,此数据对应于存储在 this.event 中的选定项目。
我将 id 从另一个组件传递给这个 book-ticket.component ,在这里我们使用

接收它
this.idByAllEvents = this.activatedRoute.snapshot.params.id;

book-ticket 组件的 HTML 部分是

 <div class="form-group">
            <label>Event</label>
            <ng-multiselect-dropdown [placeholder]="'Select the Event'" [data]="this.events" name="event"
                [(ngModel)]="this.event" [settings]="dropdownSettings1">
            </ng-multiselect-dropdown>
        </div>

.ts 部分 book-ticket 组件是

export class BookTicketComponent implements OnInit {
  user:User;
  event:Event;
  offer:Offer;
  // offer:string;
  // event:string;
  idByAllEvents: string; // This is coming from all events page

  price:number;
  percentageReduce:number; 
  offers:Offer[];
  events:Event[];
  dropdownSettings1: IDropdownSettings;
  dropdownSettings2: IDropdownSettings;
  selectedItem: any[];

  constructor(private Userservice:UserService,private Offerservice:OfferService,
    private Eventservice:EventService, private router:Router, 
    private activatedRoute:ActivatedRoute) { 
    this.idByAllEvents = this.activatedRoute.snapshot.params.id;
    console.log(this.idByAllEvents);
    this.user = new User();
    // this.offer = new Offer();
    // this.event = new Event();
    // this.offer = new Offer(); 
    // this.event = new Event();
    this.selectedItem = [];

  }


  ngOnInit() {

    this.dropdownSettings1 = {
      singleSelection: true,
      idField: '_id',
      textField: 'name',
      selectAllText: 'Select All',
      unSelectAllText: 'UnSelect All',
      itemsShowLimit: 3,
      allowSearchFilter: true,
      closeDropDownOnSelection: true,
    };



    this.Eventservice.GetEvents().subscribe(res => {
      this.events = res;
      this.justExecute();
      this.Offerservice.GetOffers().subscribe(res => {
      this.offers = res
      })
    });

 justExecute(){
    console.log(this.idByAllEvents);
    console.log(this.events);
    for (let index = 0; index < this.events.length; index++) {
      const element = this.events[index]._id;
      console.log(element);
      if(element == this.idByAllEvents){
        this.selectedItem.push(this.events[element]);
        // this.selectedItem = this.events[element]
      }
    }
    console.log(this.selectedItem);

  }




  }
SaveData(form:NgForm) {
    if(form.valid) {...}
}
}

我希望在 HTML 页面上默认选择与 this.idByAllEvents 对应的选项。但是下拉列表显示了 this.events 的所有选项。

我尝试通过函数 justExecute 设置 selectedItem,但 console.log(this.selectedItem) 给出了未定义的数组。 如果我们在 selectedItem 中获得对应于 idByAllEvents 的值,那么我们可以在 html 页面上使用[(ngModel)]="selectedItem" 来选择该值。

我们应该如何做到这一点。 有没有其他方法可以做到这一点。

【问题讨论】:

  • 在 justExecute(res) 中传递响应,然后尝试执行该功能。我猜它会起作用..试试@Asis
  • 感谢您的回复,我通过了,现在我得到了 selectedItem 中的值,但是当我设置 [(ngModel)] = "selectedItem" 时它给出了错误。
  • 你遇到了什么错误
  • 它不会自动在下拉列表中选择 selectedItem 的值。
  • selectedItem 是一个数组,其中包含一个元素作为事件对象。

标签: javascript html angular web mean-stack


【解决方案1】:

如果ng-multiselect-dropdown 用于响应式表单的FormGroup,请确保包含[ngModelOptions]='{standalone: true}'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-07
    • 2020-08-07
    • 2021-12-16
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多