【问题标题】:AngularJS : Watch for class change not workingAngularJS:注意类更改不起作用
【发布时间】:2015-09-19 16:22:21
【问题描述】:

任何想法为什么在使用 jquery 超时功能添加类时角度手表不起作用。这个想法是,当 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);
            }
        };
    });

【问题讨论】:

  • 你想达到什么目的?
  • 元素不能这样看。
  • 我的目的是:如果元素有类来触发函数
  • @ngLover 非常感谢。你能投票赞成我的问题吗

标签: javascript jquery angularjs angularjs-directive watch


【解决方案1】:

虽然建议从 angular 侧启动更改,因此 angular 会知道发生了什么,但有时您需要从外部启动也是可以理解的。

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

请参考:AngularJS access scope from outside js function

【讨论】:

    【解决方案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

    【讨论】:

    • 我的目的是:如果元素有类来触发函数,但无论如何都是 10 倍
    • 我不想在开始时看到绿色
    • @TeodorKolev - 你看小提琴了吗?
    • 这不是我的意思。我想上课。并且如果该课程似乎对某些功能很有趣
    • @TeodorKolev - 抱歉,您的问题不清楚。问任何人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多