如何将 ng-style 与 div:after 一起使用?

How do I use ng-style with div:after?

我正在使用 Angular 1.x。

这是 jsfiddle 上的 code example

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

This answer看起来和我想要的很像,但我看不懂。

谢谢。

您可以做的是创建一个单独的 class 并在其上使用 ::after 选择器。然后使用 ctrl.shouldShowDiv2 切换 class。这是一个例子:

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>