【发布时间】:2016-12-15 13:09:08
【问题描述】:
我的代码中某处有这个 div。
<div ng-class="className" >{{className}}</div>
有时我会通过 ng-click 调用 changeClass 函数来更改此 div 的类。
$scope.changeClass = function(){
console.log($scope.className)
if ($scope.className === "expand-icon")
$scope.className = "expand-icon.expanded";
else
$scope.className = "expand-icon";
};
我可以看到由于 console.log 的类更改,并且页面上输出了 {{className}}。但是似乎没有应用 css 类。
实际上是一个“+”号,需要变成一个带有动画的“-”。这是css:
/* + button */
.expand-icon {
height: 32px;
width: 32px;
position: relative;
}
.expand-icon::before, .expand-icon::after {
content: " ";
width: 32px;
height: 6px;
background-color: #fff;
display: block;
position: absolute;
top: 50%;
left: 50%;
transition: all 0.15s cubic-bezier(.42, 0, .58, 1);
opacity: 1;
border-radius: 2px;
}
.expand-icon::before {
transform: translate(-50%, -50%) rotate(90deg);
}
.expand-icon::after {
transform: translate(-50%, -50%);
}
.expand-icon {
height: 32px;
width: 32px;
position: relative;
}
.expand-icon.expanded::before {
transform: translate(-50%, -50%) rotate(0deg);
}
.expand-icon.expanded::after {
transform: translate(-50%, -50%) rotate(-90deg);
opacity: 0;
}
因此应用了 expand-icon 但 expand-icon.expanded 没有任何影响,因为我正在使用这个 ng-class 技巧。知道为什么吗?
【问题讨论】:
-
你应该用空格替换点:
$scope.className = "expand-icon expanded";
标签: javascript angularjs frontend