【问题标题】:angular 2 user can do one click on the button then it becomes disabledangular 2用户可以单击按钮然后它被禁用
【发布时间】:2017-09-26 10:02:22
【问题描述】:

您好,我正在开发 Angular 2 应用程序; 我有实体 article(id , title , text , vote) , account(id) , likes(articleid and userid) 我有一个名为 like 的按钮。 我想如果用户点击按钮就像按钮被禁用并且投票增加一样。 我做了一些有效的代码,但按钮没有被禁用。 这是我在打字稿方面的功能:

Like(Article,likes:Likes) {

    Article.vote += 1;
    this.articleService.modifier(Article)
        .subscribe((res: Article) => this.onSaveSuccess(res), (res: 
    Response) => this.onSaveError(res.json()));

   this.likes= new Likes;
   this.likes.articleid=Article.id;
    this.principal.identity().then((account) => {
        this.currentAccount = account;
    });
    this.likes.userid=this.currentAccount.firstName;
    this.likesService.create(this.likes)
        .subscribe((res: Likes) => this.onSaveSuccess2(res), (res:  
    Response) => this.onSaveError(res.json()));
}

这是 HTML 端:

   <span *ngFor="let article of articles ;trackBy: trackId">
   <div style="margin-left: 30px; padding-top: 20px;">
   <div class="row">
   <h3 >{{article.titre}}</h3><br/>
   <p >{{article.text}}</p><br/>
   <div  > <strong >Publié par</strong><p >{{article.utilisateur}}</p></div>




   <p style="float:right;padding-right: 50px;margin-left: 5px;">    
     {{article.vote}}</p>

<span *ngFor="let likes of likess ;trackBy: trackId">

  <span *ngIf="likes.articleid === article?.id &&
                   likes.userid === account.id">

         <button type= "submit"
                 (click)=" Like(article,likes)"
                 disabled

                 class="btn btn-danger btn-sm">
                        <span class="fa fa-thumbs-up"></span>

                    </button>
    </span>
    <span *ngIf="likes.articleid != article?.id || !likes.articleid  ">

         <button type= "submit"
                 (click)=" Like(article,likes)"


                 class="btn btn-danger btn-sm">
                        <span class="fa fa-thumbs-up"></span>

                    </button>
    </span>

</span>

【问题讨论】:

    标签: angular angular2-template angular2-forms angular2-services angular2-directives


    【解决方案1】:

    单击按钮时,将任何属性设置为 true,例如 Article.disabled = true,并在按钮上添加 disabled 指令,例如 [disabled]="article.disabled"

    【讨论】:

    • @Pengyy 的答案是正确的,但在这种情况下,使用单个 disableBtn 将立即禁用所有按钮。但是如果你有多篇文章并且每篇文章都有自己的like按钮,那么你需要像上面提到的那样在文章项中设置一个属性
    • 是的,但是即使我添加了一个名为 disable 的属性,它也不会保存在数据库中还有另一个问题是其他用户将无法单击该按钮
    • 不,它不会保存在数据库中。它将仅用于前端。但是,当再次从服务器获取时,您可以检查当前登录的用户是否喜欢这篇文章,然后为这个特定元素设置 disable 属性为 true,然后 Angular 会自动将按钮显示为禁用。
    • 好的,但是我将如何检查当前登录的用户是否喜欢,如果他是第一次检查页面时应该禁用按钮。
    • 您将简单地查询存储用户喜欢的表(比如哪个用户喜欢了哪篇文章)。这些信息肯定会在您的数据库中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2013-08-14
    • 2020-09-23
    • 1970-01-01
    • 2011-01-20
    相关资源
    最近更新 更多