AngularJS:注意班级变更无效

时间:2015-07-02 08:35:04

标签: javascript jquery angularjs angularjs-directive watch

当使用jquery超时函数添加类时,为什么angular watch无法正常工作的任何想法。这个想法是当jquery很难改变class:angular来捕获那个新类。小提琴中的工作实例。向下滚动时,您将看到类的更改 解决了:Fiddle

<div ng-app='myApp'>
    <div ng-controller="Ctrl" class="holder">
        <div class="pusher"></div>
        <table id="table">
            <thead>
                <tr>
                <th class="ui sticky gar" ng-repeat="friend in friends" add-remove-class>{{friend.name}}</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
       <div class="pusher"></div>
        <div class="pusher"></div>
        <div class="ui sticky" id="gar" add-remove-class>Garrrrr</div>
    </div>
    <p class="footer" ng-class="wohooOrNoo">footer</p>
</div>



 var myApp = angular.module('myApp', []);
    myApp.controller("Ctrl", function ($scope, $timeout) {
        $('#gar').sticky();
        $('gar').sticky();

       $scope.friends = [
      {name:'John', age:25, gender:'boy'},
      {name:'Jessie', age:30, gender:'girl'},
      {name:'Johanna', age:28, gender:'girl'},
      {name:'Joy', age:15, gender:'girl'},
      {name:'Mary', age:28, gender:'girl'},
      {name:'Peter', age:95, gender:'boy'},
      {name:'Sebastian', age:50, gender:'boy'},
      {name:'Erika', age:27, gender:'girl'},
      {name:'Patrick', age:40, gender:'boy'},
      {name:'Samantha', age:60, gender:'girl'}
    ]
    });

    myApp.directive('addRemoveClass', function () {
        return {
            link: function link(scope, element, attrs) {

                // create an observer instance
                var observer = new MutationObserver(function (mutations) {
                    scope.$apply(function () {
                        if (element.hasClass('fixed')) {
                            alert("wee");
                        }
                    });
                });

                // configuration of the observer:
                var config = {
                    attributes: true
                };

                // pass in the target node, as well as the observer options
                var node = element.get(0);
                observer.observe(node, config);
            }
        };
    });

2 个答案:

答案 0 :(得分:2)

在您的上下文中没有指令需要。只需在角度中使用 ng-class 即可 Docs for ngClass

HTML:

<div ng-app='myApp'>
<div ng-controller="Ctrl">
    <div>
      <span id="zzz" ng-click="toggleClass()" ng-class="{'rouge': toggle, green : !toggle}">Coucou</span>
    </div>
</div>
</div>

角:

angular.module('myApp', [])
  .controller('Ctrl', function($scope) {
         setTimeout(function() {
         $('#zzz').addClass('rouge');
       }, 2000);
      $scope.toggle = false;
      $scope.toggleClass = function() {
          $scope.toggle = !$scope.toggle;
      }
  })

Fiddle

答案 1 :(得分:1)

虽然建议从角度侧开始更改,但角度会知道发生了什么,有时你需要从外面启动它是可以理解的。

在这种情况下,角度$watch不会起作用,因为更改不是在角度本身内进行的。您需要访问元素范围并从外部手动调用scope.$apply

请参阅:AngularJS access scope from outside js function