ng-class不会触发自定义指令[重复]

问题描述 投票:-1回答:1

我创建了一个AngularJS指令,它将在所有带有类名.collapse的元素上触发。

但是当我使用Angular qazxsw poi指令添加此类时,不会触发自定义qazxsw poi指令。

演示我的问题

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>添加的类?

javascript angularjs angular-directive
1个回答
-2
投票

我相信这是directive

基本上,ng-class设置类太晚了 - 在DOM加载之后。在指令中应用条件可以在指令本身内完成以避免这个问题。在该链接中有使用属性而不是类的指令,然后在指令中进行检查。希望有所帮助。以下示例显示了以下内容:

ng-class
© www.soinside.com 2019 - 2024. All rights reserved.