angular 2用户可以单击该按钮然后它将被禁用

时间:2017-04-28 10:07:51

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

您好我正在开发angular 2 app; 我有实体文章(id,title,text,vote),account(id),likes(articleid和userid) 我有一个叫做喜欢的按钮。 我想如果用户点击按钮就像按钮变为禁用并投票增加。 我做了一些有效的代码,但按钮没有被禁用。 这是我在打字稿方面的功能:

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>

1 个答案:

答案 0 :(得分:0)

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