首页 > 解决方案 > 如何在 div:after 中使用 ng-style?

问题描述

我正在使用 Angular 1.x。

这是 jsfiddle 上的代码示例

如果ctrl.shouldShowDiv2 = false我不想div2以其他方式显示(ctrl.shouldShowDiv2 = true)我想激活box::after。有没有办法让它与ng-style或任何其他指令一起工作?

这个答案看起来与我想要的相似,但我无法理解。

谢谢。

标签: htmlcssangularjsangular-directive

解决方案


您可以做的是创建一个使用::after选择器的单独类。然后你使用你的ctrl.shouldShowDiv2. 这是一个例子:

angular.module('app', []);
.box {
  height: 150px;
  width: 200px;
  overflow: hidden;
  position: relative;
  background: tomato;
}

.box-xtra::after {
  content: "div2";
  width: 100px;
  height: 20px;
  position: absolute;
  top: 10px;
  left: -30px;
  transform: rotate(-45deg);
  background-color: white;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>
<div ng-app="app">
  <div style="margin-bottom: 10px;">
    <label>Show div2 <input type="checkbox" ng-model="ctrl.shouldShowDiv2" /></label>
  </div>
  <div class="box" ng-class="{'box-xtra': ctrl.shouldShowDiv2}"></div>
</div>


推荐阅读