【问题标题】:using ui.bootstrap causing issues with carousel使用 ui.bootstrap 导致轮播问题
【发布时间】:2014-10-13 02:42:26
【问题描述】:

我在让轮播正常工作方面遇到问题。我用 yeomen 搭建了 Angular 应用程序。 我收到此错误

Error: [$compile:ctreq] Controller 'carousel', required by directive 'slide', can't be found!
http://errors.angularjs.org/1.2.26/$compile/ctreq?p0=carousel&p1=slide
    at http://localhost:9000/bower_components/angular/angular.js:78:12
    at getControllers (http://localhost:9000/bower_components/angular/angular.js:6543:19)
    at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:6712:35)
    at http://localhost:9000/bower_components/angular/angular.js:6913:13
    at http://localhost:9000/bower_components/angular/angular.js:8113:11
    at wrappedCallback (http://localhost:9000/bower_components/angular/angular.js:11573:81)
    at wrappedCallback (http://localhost:9000/bower_components/angular/angular.js:11573:81)
    at http://localhost:9000/bower_components/angular/angular.js:11659:26
    at Scope.$eval (http://localhost:9000/bower_components/angular/angular.js:12702:28)
    at Scope.$digest (http://localhost:9000/bower_components/angular/angular.js:12514:31) <div ng-class="{
    'active': leaving || (active &amp;&amp; !entering),
    'prev': (next || active) &amp;&amp; direction=='prev',
    'next': (next || active) &amp;&amp; direction=='next',
    'right': direction=='prev',
    'left': direction=='next'
  }" class="left carousel-control item text-center ng-isolate-scope" ng-transclude="" href="#Carousel" data-slide="prev"> angular.js:10072
Error: [$compile:ctreq] Controller 'carousel', required by directive 'slide', can't be found!
http://errors.angularjs.org/1.2.26/$compile/ctreq?p0=carousel&p1=slide
    at http://localhost:9000/bower_components/angular/angular.js:78:12
    at getControllers (http://localhost:9000/bower_components/angular/angular.js:6543:19)
    at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:6712:35)
    at http://localhost:9000/bower_components/angular/angular.js:6913:13
    at http://localhost:9000/bower_components/angular/angular.js:8113:11
    at wrappedCallback (http://localhost:9000/bower_components/angular/angular.js:11573:81)
    at wrappedCallback (http://localhost:9000/bower_components/angular/angular.js:11573:81)
    at http://localhost:9000/bower_components/angular/angular.js:11659:26
    at Scope.$eval (http://localhost:9000/bower_components/angular/angular.js:12702:28)
    at Scope.$digest (http://localhost:9000/bower_components/angular/angular.js:12514:31) <div ng-class="{
    'active': leaving || (active &amp;&amp; !entering),
    'prev': (next || active) &amp;&amp; direction=='prev',
    'next': (next || active) &amp;&amp; direction=='next',
    'right': direction=='prev',
    'left': direction=='next'
  }" class="right carousel-control item text-center ng-isolate-scope" ng-transclude="" href="#Carousel" data-slide="next"> angular.js:10072

这是我的 html 文件

 <style>
            #slides_control > div{
                height: 200px;
            }
            img{
                margin:auto;
                width: 400px;
            }
            #slides_control {
                position:absolute;
                width: 400px;
                left:50%;
                top:20px;
                margin-left:-200px;
            }
            .carousel-control.right {
                background-image: linear-gradient(to right, rgba(0, 0, 0, .0001) 0%, rgba(237, 232, 232, 0.5) 100%) !important;
            }
            .carousel-control.left {
                background-image: linear-gradient(to right, rgba(249, 248, 248, 0.5) 0%, rgba(0, 0, 0, .0001) 100%) !important;
            }
        </style>
<div id="Carousel" class="carousel slide">
    <ol class="carousel-indicators">
        <li data-target="Carousel" data-slide-to="0" class="active"></li>
        <li data-target="Carousel" data-slide-to="1"></li>
        <li data-target="Carousel" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner">
        <div class="item active">
            <img src="images/sliders/main_page_slider/PhoneApp_Website_Home_41.png" class="img-responsive">
        </div>
        <div class="item">
            <img src="images/sliders/main_page_slider/PhoneApp_Website_Home_45.png" class="img-responsive">
        </div>
        <div class="item">
            <img src="images/sliders/main_page_slider/PhoneApp_Website_Home_49.png" class="img-responsive">
        </div>
    </div>
    <a class="left carousel-control" href="#Carousel" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left"></span>
    </a>
    <a class="right carousel-control" href="#Carousel" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right"></span>
    </a>
</div>

我的控制器是

'使用严格';

angular.module('myhApp') .controller('MainCtrl', function ($scope) { });

这是我的 app.js

angular
  .module('myhApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.bootstrap'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .when('/about', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

我不确定是什么原因造成的。任何帮助将不胜感激。

** 最近的一些发现 **

好的,我做了一些挖掘,发现我必须在 dom 中有轮播以获取指令(如错误所示)。当我添加轮播时,错误消失了,但我的轮播不再工作并且看起来也很不稳定。

这是我对 html 所做的更改

<div id="Carousel" class="carousel slide" carousel>

这是它的外观,您可以看到猫脸上有一个额外的箭头。我不知道怎么回事..任何帮助都会得到赞赏

【问题讨论】:

  • 我认为它与 ui.bootstrap.transition 有关系。不确定

标签: angularjs angularjs-directive angular-ui yeoman angular-ui-bootstrap


【解决方案1】:

我能够通过将 ng-non-bindable 添加到具有 data-slide 指令的元素来解决冲突。见下文:

  <a data-slide="prev" href="#clients-slider" ng-non-bindable class="left carousel-control">‹</a>

【讨论】:

  • 这个答案看起来很有希望,但是,一旦添加了不可绑定的,左右箭头的功能就会停止。不知道为什么..
  • 由于 ng-non-bindable 指令消除了 ui-bootstrap 冲突,问题可能出在您的页面上如何定义引导轮播。点击此链接获取一个简单的示例:w3schools.com/bootstrap/bootstrap_carousel.asp
  • 非常感谢 .. 经过 1 小时的代码斗争 .. 降落在这里 .. 并且 Whollla 在一秒钟内解决了
【解决方案2】:

data-slide 被 Bootstrap 和 ui.bootstrap 使用,所以这里有冲突。如果你想使用普通的 Bootstrap 轮播方法,你可以告诉 Angular 忽略 DOM 元素及其子元素。

为此,将 ngNonBindable 插入到相应的 Dom 元素中。

【讨论】:

    【解决方案3】:

    根据 gooseman 的answer on SO:

    angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
    .controller('CarouselController', ['$scope', '$timeout', '$transition', '$q', function ($scope, $timeout, $transition, $q) {
    }]).directive('carousel', [function() { 
        return { }
    }]);
    

    【讨论】:

    • 引用ng-non-bindable 属性的其他答案对我不起作用。这确实奏效了。谢谢!
    【解决方案4】:

    我有同样的问题,我不知道为什么,但如果你在 HTML 中删除 data-slide="prev"data-slide="next",错误就会消失。

    【讨论】:

    • 解释来自Melvin Gruesbeck的回答。如果你同时使用 Bootstrap 和 Angular-UI-Bootstrap 会有冲突。可以使用@ozkary 示例解决它,而不是放弃删除数据幻灯片属性;)
    【解决方案5】:

    如果您不需要,只需将 ui.bootstrap 从模块列表中删除即可。

    【讨论】:

      【解决方案6】:

      添加 ng-non-bindable 并更改 data-target 的 href

      <a class="left carousel-control" data-target="#myCarousel" role="button" data-slide="prev" ng-non-bindable><</a>
      <a class="right carousel-control" data-target="#myCarousel" role="button" data-slide="next" ng-non-bindable>></a>
      

      【讨论】:

      • 谢谢!我找到了ng-non-bindable,但由于href,它仍然无法正常工作。谢谢!
      【解决方案7】:

      使用 Angular 构建时忘记引导程序的 javascript。仅使用 css。尤其是在使用 ngRoute 时。它会从 url 中读取哈希值。对于轮播,请使用 ui.bootstrap.carousel,阅读文档并查看示例page。简单明了。否则控制台总会报错,那就是轮播工作正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-25
        • 1970-01-01
        • 1970-01-01
        • 2013-03-17
        • 2014-12-30
        相关资源
        最近更新 更多