【发布时间】:2018-06-13 07:46:48
【问题描述】:
我创建了一个AngularJS 指令,该指令将在所有具有类名 .collapse 的元素上触发。
但是当我使用 Angular 的 ng-class 指令添加这个 类 时,不会触发自定义的 collapse 指令。
我的问题演示
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.welcome = "model";
$scope.isTrue = function() {
return true;
}
});
app.directive("directive", function($compile) {
return {
restrict: 'C',
scope: {
model: '@'
},
link: function(scope, elem, attrs) {
elem.css("background-color", "blue");
}
}
});
.directive {
background-color: red;
color: white;
padding: 10px;
margin-bottom: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<p>Simple Angular app</p>
<div class="directive">Make my background color blue using the directive</div>
<div ng-class="isTrue() ? 'directive' : ''">Make my background color blue using the directive</div>
</div>
如何使directive 也触发使用ng-class 添加的类?
【问题讨论】:
标签: javascript angularjs angular-directive