【问题标题】:Removing the clicked item from menu list using angular 4使用角度 4 从菜单列表中删除单击的项目
【发布时间】:2017-12-07 21:38:52
【问题描述】:

你好,

我有一个通知菜单列表,我在其中绑定来自 REST API 的数据。我的通知菜单将如下所示。为清楚起见,请参见下图。

在通知中,当我单击任何列表项时,它应该从列表中删除,并且在上面还有一个红色的通知气泡,其中包含我收到的通知数量。我想要实现的是,当我单击通知菜单列表时,应从其中删除单击的列表项,并且在铃铛图标上方的红色气泡中计数应减少。例如,当我单击第一项的通知列表项时,应将其删除并减少计数。

notification.component.ts

try {

      this.currentUser = JSON.parse(this.cookieService.get('user-is-logged-in-details'));
      console.log(this.currentUser.uid);
      this._userService.getBellNotification(this.currentUser.uid)
      .subscribe(data => this.cookieData = data); 
      } catch (e) {
          console.log("Catch Block Error from Notification.Component.ts");
          //console.log("currentUser", this.currentUser.uid);
      }

apiservice.service.ts

getBellNotification(uid: any){
        const urlBell = this.config.apiUrl+'api/supplier-notification/'+uid+'?_format=json';
        let headers = new Headers();        
        headers.append("Content-Type", 'application/json');
        headers.append('Authorization', 'Basic ' + btoa('apiuser:@piUser@2017Supplier'));
        let requestoptions = new RequestOptions({headers: headers});
        return this.http.get(urlBell, requestoptions)
        .map(response => response.json())
        .catch(this.handleErrorObservable);
    }

notification.component.html

<span 
    class="notification" 
    dropdown (onShown)="onShown()" 
    (onHidden)="onHidden()" 
    (isOpenChange)="isOpenChange()">
    <a
        href dropdownToggle 
        (click)="false">
        <i 
        class="glyphicon glyphicon-bell" 
        [ngClass]="cookieData?.search_unread_count != 0 ? 'notification-icon': ' '">
    <span *ngIf="cookieData?.search_unread_count != 0">{{cookieData.search_unread_count}}</span> 
    </i>
    </a>
      <ul 
        *dropdownMenu 
        class="dropdown-menu notify-drop">
          <div 
            class="notify-drop-title">Notifications</div>
            <div 
                class="drop-content" 
                *ngFor="let item of cookieData.search_result">
                <li 
                    data-id="{{item.id}}" 
                    class="notification-items unread">  
                    <i 
                        class="fa fa-dot-circle-o" 
                        aria-hidden="true">
                    </i>
                    <a 
                        data-link="admin-invoice-list" 
                        href="javascript:;">{{item.message}}
                    </a>
                </li>
            </div>
      </ul>
</span>

谁能建议我如何实现它?

【问题讨论】:

    标签: arrays angular typescript


    【解决方案1】:

    @federico scamuzzi ise 解释了你到底错过了什么。但他的回答是需要对您的对象和要求进行一些修正。我在这里更新了,试试看,如果你有任何澄清,请告诉我。

               <div 
                class="drop-content" 
                *ngFor="let item of cookieData.search_result">
                <li (click)="remove(item)"
                    data-id="{{item.id}}" 
                    class="notification-items unread">  
                    <i 
                        class="fa fa-dot-circle-o" 
                        aria-hidden="true">
                    </i>
                    <a 
                        data-link="admin-invoice-list" 
                        href="javascript:;">{{item.message}}
                    </a>
                </li>
    

    你的删除功能应该是

    remove(item:any){
        //ALSO CALL A BACKEND API... TO REMOVE THEM .. THIS IS ONLY FRONT END
        this.cookieData.search_result = this.cookieData.search_result.filter(xx=>xx.id != item.id);
        this.cookieData.search_unread_count=this.cookieData.search_result.length;
    
        }
    

    【讨论】:

    • Ramesh,remove 方法中的 xx 是什么?
    • arrow functionlamda expression一样在谷歌搜索
    • 我们如何在模板中绑定来自json的原始html数据?请参阅此链接以供参考。 stackoverflow.com/questions/47708894/…
    • @youi 这个问题和答案怎么样?我应该关闭这个问题到on hold吗?
    【解决方案2】:

    您是否尝试在每个上附加(点击)功能

  • 以便在点击
  • 时删除(或执行您想要的操作)
  • 项目?...类似于:
     <div 
                    class="drop-content" 
                    *ngFor="let item of cookieData.search_result">
                    <li (click)="remove(item)"
                        data-id="{{item.id}}" 
                        class="notification-items unread">  
                        <i 
                            class="fa fa-dot-circle-o" 
                            aria-hidden="true">
                        </i>
                        <a 
                            data-link="admin-invoice-list" 
                            href="javascript:;">{{item.message}}
                        </a>
                    </li>
    

    然后在你的ts file:

       remove(item:any){
        //ALSO CALL A BACKEND API... TO REMOVE THEM .. THIS IS ONLY FRONT END
        this.cookieData = this.cookieData.filter(xx=>xx.id != item.id);
          this.cookieData.search_unread_count=this.cookieData.search_result.length;
        }
    

    希望对你有帮助!!

  • 【讨论】:

    • 使用过滤器从数组中只删除一个元素似乎有点矫枉过正,不是吗?我认为拼接会更合适?
    • 是的,当然你也可以使用 splice .. findIndex 等等 .. 这只是关于如何点击点击的项目的建议
    猜你喜欢
    • 2017-12-28
    • 2016-09-21
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2013-04-23
    • 2023-03-11
    • 1970-01-01
    • 2023-02-10
    相关资源
    最近更新 更多