Search code examples
angulartypescriptangular2-templateangular2-formsangular2-services

How to set angular (Select all ) in side the dropdown


I am trying to develop a dropdown in my Angular application. The Drop Down is shop list. When I select a shop it will show or (display) the content in the shop.so Here I want a new item in shop list (item is ALL SHOP) inside the dropdown..Like this

enter image description here here the data(shopslists) are taking from Database(angular with web api)

This is my Ts code

 ngOnInit() {

            this.Service.FetchPopulateOutlets().subscribe(outletsData => this.outletDetails = outletsData,
                error => {


 console.error(error);
                this.statusMessage = "Problem with the service.Please try again after sometime";
            });
    onSelect(shopid: number) {
    this.Service.FetchItemDetails(shopid, this.pageIndex).subscribe(itemsData => this.itemdetails = itemsData,
                        error => {
                            console.error(error);
                            this.statusMessage = "Problem with the service.Please try again after sometime";
                        });

This is my HTML

  <span>
            <select class="formcontrol" name="outletDetail" (change)="onSelect($event.target.value)">
                <option value="0" disabled>Select a Shop</option>
                <option *ngFor="let outletDetail of outletDetails" value={{outletDetail.ShopID}}>{{outletDetail.ShopName}}</option>
            </select>

Solution

  • ngOnInit() {
        this.Service.FetchPopulateOutlets().subscribe(outletsData => {
            let allShops = {
                ShopName: 'All',
                ShopID: 0
            }
            this.outletDetails = [allShops,...outletsData];
        }, error => {
            console.error(error);
            this.statusMessage = "Problem with the service.Please try again after sometime";
        });
        onSelect(shopid: number) {
            if (shopId == 0) {
                "Insert your logic here"
            }
            this.Service.FetchItemDetails(shopid, this.pageIndex).subscribe(itemsData => this.itemdetails = itemsData,
                error => {
                    console.error(error);
                    this.statusMessage = "Problem with the service.Please try again after sometime";
                });
        }