Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS:注意换班不起作用_Javascript_Jquery_Angularjs_Angularjs Directive_Watch - Fatal编程技术网

Javascript AngularJS:注意换班不起作用

Javascript AngularJS:注意换班不起作用,javascript,jquery,angularjs,angularjs-directive,watch,Javascript,Jquery,Angularjs,Angularjs Directive,Watch,知道为什么angular watch在类中添加jquery超时函数时不起作用吗。其思想是,当jquery被要求更改类时:使用angular来捕获新类。在小提琴中工作的例子。当你向下滚动时,你会看到类的变化 已解决: {{friend.name} 加勒尔 页脚 var myApp=angular.module('myApp',[]); 控制器(“Ctrl”,函数($scope,$timeout){ $('#gar').sticky(); $('gar').sticky(); $scope.fri

知道为什么angular watch在类中添加jquery超时函数时不起作用吗。其思想是,当jquery被要求更改类时:使用angular来捕获新类。在小提琴中工作的例子。当你向下滚动时,你会看到类的变化 已解决:


{{friend.name}
加勒尔
页脚

var myApp=angular.module('myApp',[]); 控制器(“Ctrl”,函数($scope,$timeout){ $('#gar').sticky(); $('gar').sticky(); $scope.friends=[ {姓名:'John',年龄:25岁,性别:'boy'}, {姓名:'Jessie',年龄:30岁,性别:'girl'}, {姓名:'Johanna',年龄:28岁,性别:'girl'}, {姓名:'Joy',年龄:15岁,性别:'girl'}, {姓名:'Mary',年龄:28岁,性别:'girl'}, {姓名:'Peter',年龄:95岁,性别:'boy'}, {姓名:'Sebastian',年龄:50岁,性别:'boy'}, {姓名:'Erika',年龄:27岁,性别:'girl'}, {姓名:'Patrick',年龄:40岁,性别:'boy'}, {姓名:'Samantha',年龄:60岁,性别:'girl'} ] }); myApp.directive('addRemoveClass',函数(){ 返回{ 链接:功能链接(范围、元素、属性){ //创建一个观察者实例 var观察者=新的突变观察者(功能(突变){ 作用域:$apply(函数(){ if(element.hasClass('fixed')){ 警报(“wee”); } }); }); //观察员的配置: 变量配置={ 属性:true }; //传入目标节点以及观察者选项 var node=element.get(0); observer.observe(节点,配置); } }; });
在您的上下文中,不需要任何指令。只需在angular中使用ng class

html:


虽然建议从角度进行更改,以便角度可以知道发生了什么,但有时您需要从外部进行更改是可以理解的

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


请参阅:

您想要实现什么?元素不能像这样被观看。我的目的是:如果元素有类来激发function@ngLover非常感谢。你们能投票支持我的问题吗?我的目的是:若元素有类触发函数,但无论如何是10倍,我不想看到绿色beggining@TeodorKolev-你看小提琴了吗?这与我的观点不符。我想在上课时看电视。如果那门课看起来很有趣的话function@TeodorKolev-对不起,你的问题不清楚。问任何人
<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);
            }
        };
    });
<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;
      }
  })