Twitter bootstrap 如何绑定到Angular UI';s旋转滑梯活动?

Twitter bootstrap 如何绑定到Angular UI';s旋转滑梯活动?,twitter-bootstrap,angular-ui,angular-ui-bootstrap,Twitter Bootstrap,Angular Ui,Angular Ui Bootstrap,我正在使用AngularUI的旋转木马,我需要告诉我的谷歌图表在它们滑入视图后重新绘制。尽管我读了很多书,但我似乎无法参与这件事 请参阅我的尝试: HTML: 它应该是这样工作的: -无角用户界面转盘 也许有一种更具角度的方式来说明我的图表已经滑入视野了?我可以考虑三种方式,这取决于您的要求 请参见示例 使用$scope.$watch查看单个幻灯片,以检查其是否处于活动状态。 使用带有自定义功能的$scope.$watch查找活动幻灯片。 然后像这样使用它: 在html模板中: .

我正在使用AngularUI的旋转木马,我需要告诉我的谷歌图表在它们滑入视图后重新绘制。尽管我读了很多书,但我似乎无法参与这件事

请参阅我的尝试:

HTML:

它应该是这样工作的: -无角用户界面转盘


也许有一种更具角度的方式来说明我的图表已经滑入视野了?

我可以考虑三种方式,这取决于您的要求

请参见示例

  • 使用$scope.$watch查看单个幻灯片,以检查其是否处于活动状态。

  • 使用带有自定义功能的$scope.$watch查找活动幻灯片。

    然后像这样使用它:

    在html模板中:

    
    ...
    

  • 希望得到以下帮助:)

    根据runTarm给出的答案,如果您想知道下一张幻灯片的索引,您应该添加如下内容:

     .directive('onCarouselChange', function ($parse) {
        return {
          require: 'carousel',
              link: function (scope, element, attrs, carouselCtrl) {
                var fn = $parse(attrs.onCarouselChange);
                var origSelect = carouselCtrl.select;
                carouselCtrl.select = function (nextSlide, direction,nextIndex) {
          if (nextSlide !== this.currentSlide) {
            fn(scope, {
              nextSlide: nextSlide,
              direction: direction,
              nextIndex:this.indexOfSlide(nextSlide)
            });
          }
          return origSelect.apply(this, arguments);
        };
      }
      };
    })
    
    然后,在控制器中,您只需执行以下操作即可捕获新索引:

    $scope.onSlideChanged = function (nextSlide, direction, nextIndex) {
        console.log(nextIndex);
    }
    

    我设法修改了runTarm的答案,以便在幻灯片滑入视图(即滑动动画完成)后调用回调。这是我的密码:

    .directive('onCarouselChange', function ($animate, $parse) {
        return {
            require: 'carousel',
            link: function (scope, element, attrs, carouselCtrl) {
                var fn = $parse(attrs.onCarouselChange);
                var origSelect = carouselCtrl.select;
                carouselCtrl.select = function (nextSlide, direction) {
                    if (nextSlide !== this.currentSlide) {
                        $animate.on('addClass', nextSlide.$element, function (elem, phase) {
                            if (phase === 'close') {
                                fn(scope, {
                                    nextSlide: nextSlide,
                                    direction: direction,
                                });
                                $animate.off('addClass', elem);
                            }
                        });
                    }
                    return origSelect.apply(this, arguments);
                };
            }
        };
    });
    

    秘密在于使用$animate的事件处理程序在动画完成后调用我们的函数。

    这里有一种使用控制器的替代方法,它位于runTarm的#2和#3之间

    原始HTML+新div:

    <carousel id="myC" interval="myInterval">
      <slide ng-repeat="slide in slides" active="slide.active">
        <div ng-controller="SlideController"> <!-- NEW DIV -->
          <img ng-src="{{slide.image}}" style="margin:auto;">
          <div class="carousel-caption">
            <h4>Slide {{$index}}</h4>
            <p>{{slide.text}}</p>
          </div>
        </div>
      </slide>
    </carousel>
    

    AngularUI Bootstrap更改了控制器的命名约定,因为他们在所有控制器前都加了前缀
    uib
    ,因此下面是由
    runTarm
    提供的原始解决方案的更新解决方案:

    角度:

    .directive('onCarouselChange', function($parse) {
        return {
            require: '^uibCarousel',
            link: function(scope, element, attrs, carouselCtrl) {
                var fn = $parse(attrs.onCarouselChange);
                var origSelect = carouselCtrl.select;
                carouselCtrl.select = function(nextSlide, direction, nextIndex) {
                    if (nextSlide !== this.currentSlide) {
                        fn(scope, {
                            nextSlide: nextSlide,
                            direction: direction,
                            nextIndex: this.indexOfSlide(nextSlide)
                        });
                    }
                    return origSelect.apply(this, arguments);
                };
            }
        };
    });
    
    module App.Directive {
    
        export class CarouselChange implements ng.IDirective {
    
            public require: string = '^uibCarousel';
    
            constructor(private $parse: ng.IParseService) { }
    
            public link: ng.IDirectiveLinkFn = (scope: ng.IScope, element: ng.IAugmentedJQuery, attributes: any, carouselCtrl: any) => {
                var fn = this.$parse(attributes.carouselChange);
                var origSelect = carouselCtrl.select;
                carouselCtrl.select = function(nextSlide, direction) {
                    if (nextSlide !== this.currentSlide) {
                        fn(scope, {
                            nextSlide: nextSlide,
                            direction: direction
                        });
                    }
                    return origSelect.apply(this, arguments);
                };
            }
    
            static Factory(): ng.IDirectiveFactory {
                var directive: ng.IDirectiveFactory = ($parse: ng.IParseService) => new CarouselChange($parse);
                directive['$inject'] = ["$parse"];
                return directive;
            }
        }
    }
    
    <carousel interval="15000">
      <slide>
       <video class="img-responsive-upscale" video-auto-ctrl loop preload="metadata">
        <source src=
    ...
    
    带字体脚本的角度:

    .directive('onCarouselChange', function($parse) {
        return {
            require: '^uibCarousel',
            link: function(scope, element, attrs, carouselCtrl) {
                var fn = $parse(attrs.onCarouselChange);
                var origSelect = carouselCtrl.select;
                carouselCtrl.select = function(nextSlide, direction, nextIndex) {
                    if (nextSlide !== this.currentSlide) {
                        fn(scope, {
                            nextSlide: nextSlide,
                            direction: direction,
                            nextIndex: this.indexOfSlide(nextSlide)
                        });
                    }
                    return origSelect.apply(this, arguments);
                };
            }
        };
    });
    
    module App.Directive {
    
        export class CarouselChange implements ng.IDirective {
    
            public require: string = '^uibCarousel';
    
            constructor(private $parse: ng.IParseService) { }
    
            public link: ng.IDirectiveLinkFn = (scope: ng.IScope, element: ng.IAugmentedJQuery, attributes: any, carouselCtrl: any) => {
                var fn = this.$parse(attributes.carouselChange);
                var origSelect = carouselCtrl.select;
                carouselCtrl.select = function(nextSlide, direction) {
                    if (nextSlide !== this.currentSlide) {
                        fn(scope, {
                            nextSlide: nextSlide,
                            direction: direction
                        });
                    }
                    return origSelect.apply(this, arguments);
                };
            }
    
            static Factory(): ng.IDirectiveFactory {
                var directive: ng.IDirectiveFactory = ($parse: ng.IParseService) => new CarouselChange($parse);
                directive['$inject'] = ["$parse"];
                return directive;
            }
        }
    }
    
    <carousel interval="15000">
      <slide>
       <video class="img-responsive-upscale" video-auto-ctrl loop preload="metadata">
        <source src=
    ...
    

    谢谢,

    如果您只想在幻灯片进入视图时开始播放视频,然后在幻灯片离开时暂停,请执行以下操作:

    JS

    {# Uses angular v1.3.20 & angular-ui-bootstrap v0.13.4 Carousel #}
    {% addtoblock "js" %}<script type="text/javascript">
    angular.module('videoplay', []).directive('videoAutoCtrl', function() {
      return {
        require: '^carousel',
        link: function(scope, element, attrs) {
          var video = element[0];
          function setstate(visible) {
            if(visible) {
              video.play();
            } else {
              video.pause();
            }
          }
          // Because $watch calls $parse on the 1st arg, the property doesn't need to exist on first load
          scope.$parent.$watch('active', setstate);
        }
      };
    });
    </script>{% endaddtoblock %}
    {% addtoblock "ng-requires" %}videoplay{% endaddtoblock %}
    
    {使用angular v1.3.20和angular ui引导v0.13.4旋转木马}
    {%addtoblock“js”%}
    角度.module('videoplay',[])指令('videoAutoCtrl',function(){
    返回{
    要求:“^carousel”,
    链接:函数(范围、元素、属性){
    var video=元素[0];
    功能设置状态(可见){
    如果(可见){
    video.play();
    }否则{
    video.pause();
    }
    }
    //因为$watch在第一个参数上调用$parse,所以在第一次加载时不需要存在该属性
    范围:$parent.$watch('active',setstate);
    }
    };
    });
    {%endaddtoblock%}
    {%addtoblock“ng需要“%}视频播放{%endaddtoblock%}
    
    注意:Django有额外的位

    HTML:

    .directive('onCarouselChange', function($parse) {
        return {
            require: '^uibCarousel',
            link: function(scope, element, attrs, carouselCtrl) {
                var fn = $parse(attrs.onCarouselChange);
                var origSelect = carouselCtrl.select;
                carouselCtrl.select = function(nextSlide, direction, nextIndex) {
                    if (nextSlide !== this.currentSlide) {
                        fn(scope, {
                            nextSlide: nextSlide,
                            direction: direction,
                            nextIndex: this.indexOfSlide(nextSlide)
                        });
                    }
                    return origSelect.apply(this, arguments);
                };
            }
        };
    });
    
    module App.Directive {
    
        export class CarouselChange implements ng.IDirective {
    
            public require: string = '^uibCarousel';
    
            constructor(private $parse: ng.IParseService) { }
    
            public link: ng.IDirectiveLinkFn = (scope: ng.IScope, element: ng.IAugmentedJQuery, attributes: any, carouselCtrl: any) => {
                var fn = this.$parse(attributes.carouselChange);
                var origSelect = carouselCtrl.select;
                carouselCtrl.select = function(nextSlide, direction) {
                    if (nextSlide !== this.currentSlide) {
                        fn(scope, {
                            nextSlide: nextSlide,
                            direction: direction
                        });
                    }
                    return origSelect.apply(this, arguments);
                };
            }
    
            static Factory(): ng.IDirectiveFactory {
                var directive: ng.IDirectiveFactory = ($parse: ng.IParseService) => new CarouselChange($parse);
                directive['$inject'] = ["$parse"];
                return directive;
            }
        }
    }
    
    <carousel interval="15000">
      <slide>
       <video class="img-responsive-upscale" video-auto-ctrl loop preload="metadata">
        <source src=
    ...
    
    
    
    您应该能够使用指令来钩住引导事件。午饭后我会试试看它是否能工作。所以我进一步研究了一下,你没有得到这个事件的原因是因为BootstrapUI没有为carousel小部件使用引导JavaScript。所以,甚至没有什么能引发这一事件。这帮了大忙!谢谢。如果同一页面中有多个旋转木马,则使用指令(第三个选项)更有效。不幸的是,这些方法无助于在动画转换完成后捕获幻灯片更改事件,该事件由原始引导库中的slide.bs.carousel事件触发。。。我仍然在寻找如何用角度捕捉它。这对我不起作用;我得到
    TypeError:this.indexOfSlide不是一个函数
    。它适用于select函数,但出于某些奇怪的原因,不是next或prev函数。就我所知,它应该是一样的,你知道有没有解决办法?我需要知道他们什么时候点击下一个,而不是幻灯片什么时候真正改变。这也不起作用,尽管我不确定这是否是因为我正在为Angular使用更新版本的Twitter引导(我需要使用
    uib
    )。我得到
    TypeError:无法读取第一行$animate上未定义的
    属性“0”。好的,我认为这是对Angular的更改,因为我使用的是1.5.5;您需要从
    nextSlide.$element
    中删除
    $
    ,使其成为
    nextSlide.element
    。我想这就是我要做的所有改变。
    {# Uses angular v1.3.20 & angular-ui-bootstrap v0.13.4 Carousel #}
    {% addtoblock "js" %}<script type="text/javascript">
    angular.module('videoplay', []).directive('videoAutoCtrl', function() {
      return {
        require: '^carousel',
        link: function(scope, element, attrs) {
          var video = element[0];
          function setstate(visible) {
            if(visible) {
              video.play();
            } else {
              video.pause();
            }
          }
          // Because $watch calls $parse on the 1st arg, the property doesn't need to exist on first load
          scope.$parent.$watch('active', setstate);
        }
      };
    });
    </script>{% endaddtoblock %}
    {% addtoblock "ng-requires" %}videoplay{% endaddtoblock %}
    
    <carousel interval="15000">
      <slide>
       <video class="img-responsive-upscale" video-auto-ctrl loop preload="metadata">
        <source src=
    ...