Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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
Html 如何使用ng click功能更改按钮颜色?_Html_Css_Angularjs_Angular Ui Bootstrap - Fatal编程技术网

Html 如何使用ng click功能更改按钮颜色?

Html 如何使用ng click功能更改按钮颜色?,html,css,angularjs,angular-ui-bootstrap,Html,Css,Angularjs,Angular Ui Bootstrap,我从资源中心得到了按钮列表。它由10个按钮组成,如 t1,t2,t3,t4,t5,t6,t7,t8,t9,t10 当我第一次点击t1按钮时,按钮颜色变为红色。第二次我点击相同的按钮时,颜色变为绿色 请告诉我怎么做 <div class="col-sm-2 col-lg-1 col-md-1" ng-repeat="table in tables" style="margin-left:1px"> <button type="button" class="btn btn-succ

我从资源中心得到了按钮列表。它由10个按钮组成,如 t1,t2,t3,t4,t5,t6,t7,t8,t9,t10

当我第一次点击t1按钮时,按钮颜色变为红色。第二次我点击相同的按钮时,颜色变为绿色

请告诉我怎么做

<div class="col-sm-2 col-lg-1 col-md-1" ng-repeat="table in tables" style="margin-left:1px">
<button type="button" class="btn btn-success" ng-click="getTable(table)">{{table.tablename}}</button>
</div>

我的建议是:在
$scope.ng中单击=gettable(table){……}

  • 创建一个颜色数组
  • 每次单击按钮时,在该数组中拉一个随机颜色值

    • 我的建议是:在
      $scope.ng中单击=gettable(table){……}

      • 创建一个颜色数组
      • 每次单击按钮时,在该数组中拉一个随机颜色值

        • 这很简单。只需将多个属性与表数组中的所有项绑定在一起,如下所示-

          for(var i = 0; i < $scope.tables.length; i++)
          {
              $scope.tables[i].btnClass = "btn-success";
          }
          
          或者,如果要更改手动颜色,请在css中创建一个类,如下所示:

          <button type="button" class="btn {{table.btnClass}}" ng-click="getTable(table)">
          {{table.tablename}}</button>
          
          .red-button {
              background-color: "red";
          }
          
          然后在函数中

          $scope.getTable = function(table) {
              table.btnClass = table.btnClass == "red-button" ? "btn-success" : "red-button"
          }
          

          这很容易。只需将多个属性与表数组中的所有项绑定在一起,如下所示-

          for(var i = 0; i < $scope.tables.length; i++)
          {
              $scope.tables[i].btnClass = "btn-success";
          }
          
          或者,如果要更改手动颜色,请在css中创建一个类,如下所示:

          <button type="button" class="btn {{table.btnClass}}" ng-click="getTable(table)">
          {{table.tablename}}</button>
          
          .red-button {
              background-color: "red";
          }
          
          然后在函数中

          $scope.getTable = function(table) {
              table.btnClass = table.btnClass == "red-button" ? "btn-success" : "red-button"
          }
          

          使用controllerAs语法(我建议您使用),您可以处理以下内容

          html:


          使用controllerAs语法(我建议您使用),您可以使用以下内容

          html:

          现在您可以在这里定义css规则了

          .green{
            color:green;
          }
          .blue{
            color:blue;
          }
          .red{
            color:red;
          }
          .yellow{
            color:yellow;
          }
          
          然后在视图中,您可以在要更改颜色的按钮上设置ng样式,并将其绑定到范围中的颜色,如

          <button ng-style="color">Change color</button>
          
          改变颜色
          
          现在您可以在这里定义css规则了

          .green{
            color:green;
          }
          .blue{
            color:blue;
          }
          .red{
            color:red;
          }
          .yellow{
            color:yellow;
          }
          
          然后在视图中,您可以在要更改颜色的按钮上设置ng样式,并将其绑定到范围中的颜色,如

          <button ng-style="color">Change color</button>
          
          改变颜色
          
          感谢您的快速响应。第一次单击按钮btn信息被激活。但我第二次点击同一个按钮时,btn success不适用。这里如何设置按钮的默认颜色为绿色。对于不适用于甲烷的循环,请快速响应。第一次单击按钮btn info被激活。但我第二次点击同一个按钮时,btn success不适用。这里如何设置按钮的默认颜色为绿色。对于循环不适用于Me如何为按钮设置默认颜色绿色Well只需使用按钮上的样式属性。。。{{table.tablename}}如何为按钮设置默认的绿色只需使用按钮上的style属性。。。{{table.tablename}