Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Angular 2中添加样式?_Javascript_Jquery_Angular_Typescript - Fatal编程技术网

Javascript 如何在Angular 2中添加样式?

Javascript 如何在Angular 2中添加样式?,javascript,jquery,angular,typescript,Javascript,Jquery,Angular,Typescript,嗨,我正在Angular 2开发web应用程序。我有一个下拉控件。在java脚本中,它运行良好。我正试图用Angular 2重写它。下面是我的html代码 <div class="btn-group" (click)="toggleDropdown()"> <button class="btn-toggle" style="width:151px;" type="button" >Select USer Role</button>

嗨,我正在Angular 2开发web应用程序。我有一个下拉控件。在java脚本中,它运行良好。我正试图用Angular 2重写它。下面是我的html代码

<div class="btn-group" (click)="toggleDropdown()">
          <button class="btn-toggle" style="width:151px;" type="button" >Select USer Role</button>
                  <ul class="dropdown-menu" role="menu" style="width:100%;">
                      <li *ngFor="let userrole of userroles; let i = index" [value]="userrole.userroleid">
                          <div style="margin-left:3px">
                               <label style="margin-left:2px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox17"> {{userrole.username}}</label>
                           </div>
                       </li>
                  </ul>
</div>
我试了如下。我添加了click事件(click)=“toggleDropdown()”。在TS文件中我添加了

toggleDropdown() {
        this._showStyle = true;
  }
然后我添加了下面的html代码

[ngClass]="{'open': _showStyle }"
这个很好用。在我用Javascript编写下面的代码时,我正在努力删除样式

  $(".dropdown-menu li").on("click", function () {

                $('.btn-toggle').text($(this).text());
                $('.dropdown-menu').removeClass('open');
            });

我正试着把上面的陈述写在第二章中。有人能帮我做这件事吗?任何帮助都将不胜感激。谢谢

您可以创建布尔值。例如,
让isDropDownOpen=false

然后功能将是:

toggleDropdown() {
   return !this.isDropDownOpen;
}
然后在html中,您可以执行以下操作:

<ul class="dropdown-menu" role="menu" style="width:100%;" [ngClass]="{'open': isDropDownOpen}">
</ul>



您试过了吗?你能分享你的角度代码吗?请分享你的角度代码,这将更有助于理解。看看这个链接()谢谢。我更新了密码谢谢。添加类很好。我正在寻找$('.btn toggle').text($(this.text());相当于javascript。这不是编辑问题之前的情况。您的问题是关于添加类。无论如何,您可以使用angular.io/api/core/ViewChild访问btn toggle元素,然后使用基本javascript操作分配文本。或者您可以使用指令访问elementRef.io/api/core/elementRef–
<ul class="dropdown-menu" role="menu" style="width:100%;" [ngClass]="{'open': isDropDownOpen}">
</ul>