简体   繁体   English

使用AngularJS控制CSS

[英]Controlling CSS using AngularJS

I am trying to control CSS through AngularJS. 我试图通过AngularJS控制CSS。 I am using ng-class and ng-click for this purpose ..... I have made two function showError and showWarning for this purpose. 我为此使用ng-class和ng-click .....为此,我制作了两个函数showError和showWarning。 I am try to change CSS through setting their properties true and false respectively 我试图通过分别设置属性true和false来更改CSS

--------CSS--------- -------- CSS ---------

.error {
      background-color: red;
  }
.warning {
         background-color: orange;
  }

-------HTML--------- ------- HTML ---------

<div>
  <h2>  {{ message }} </h2>
   <ul>
       <li class="menu-disabled-{{ isDisabled }}" ng-click="stun()"> Stun</li>
   </ul>
    <div class='{ warning : {{ isWarning }}, error : {{ isError }} }'>
    {{ messageText }}</div>
    <button ng-click="showError()"> Simulate Error</button>
    <button ng-click="showWarning()">Simulate Warning</button>


</div>

-------JS----------- ------- JS -----------

  $scope.isError = false;
     $scope.isWaring = false;

     $scope.showError = function(){
         console.log("error here");
         $scope.messageText = "This is an error";
         $scope.isError = true;
         $scope.isWaring = false;
     }//showError

     $scope.showWarning = function() {
         console.log("warning here");
         $scope.messageText = "Just a Warning, Carry ON !!!";
         $scope.isError = false;
         $scope.isWaring = true;
     }//showWarning

I am successful to access the function and printing the messageText using ng-click but can't change the CSS properties//// 我可以使用ng-click成功访问该函数并打印messageText,但是无法更改CSS属性////

Angular way is making that in template: 角度的方式是在模板中做到这一点:

<div>
    <h2>  {{ message }} </h2>
    <ul>
        <li ng-class="{'menu-disabled': isDisabled}" ng-click="stun()"> Stun</li>
    </ul>
    <div ng-show="isError" class="error">{{ errorMessage }}</div>
    <div ng-show="isWarning" class="warning">{{ warningMessage }}</div>

    <button ng-click="isError=true;isWarning=false"> Simulate Error</button>
    <button ng-click="isWarning=true;isError=false">Simulate Warning</button>

</div>

No javascript code is needed and advisely, you shouldn't use javascript for that kind of view manipulations. 不需要javascript代码,建议您不要将javascript用于这种视图操作。

EDIT: You can use scope variables for message texts instead of static texts. 编辑:您可以将范围变量用于消息文本,而不是静态文本。

it should be like this 应该是这样

<div>
  <h2>  {{ message }} </h2>
   <ul>
       <li class="'menu-disabled' : isDisabled" ng-click="stun()"> Stun</li>
  </ul>     
<div class='error' ng-show="isDisabled" > {{ messageText }} </div>
<div class='warning' ng-show="isWarning" > {{ messageText }} </div>
<button ng-click="showError()"> Simulate Error</button>
<button ng-click="showWarning()">Simulate Warning</button>


   </div>

I have found the solution, We have to pass boolean parameter in css also : 我已经找到了解决方案,我们还必须在css中传递布尔参数:

.warning-true{
         background-color: orange;
  }
.error-true{
    background-color: red;
}

 <div class=" warning-{{ isWarning }} , error-{{ isError }} ">{{ messageText }}  </div>
    <button ng-click="showError()"> Simulate Error</button>
    <button ng-click="showWarning()">Simulate Warning</button> 

and js reamin same. 和js reamin一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM