Angular js错误:[$compile:ctreq]指令yyyy要求的控制器xxxx,无法找到

Angular js Error: [$compile:ctreq] Controller xxxx, required by directive yyyy, can't be found

本文关键字:控制器 xxxx yyyy 错误 js compile 指令 ctreq Angular      更新时间:2023-09-26

我已经在stackoverflow上有多个主题关于这个错误,但我总是不理解我的上下文中的问题。

我有这样的模板:

<div class="progress">
<div class="progress-bar"
     data-toggle="tooltip"
     title="Parts offertes : {{ '{{ gift.gived_parts}}' }} / {{ '{{ gift.number_of_parts }}' }}"
     data-bar="gived"
     >
    <span ng-class="{true:'op7', false:''}[gift.percent_done > 0 && gift.percent_done < 20]">
        {{ '{{ gift.gived_parts}}' }}
    </span>
</div>
<div class="progress-bar progress-bar-danger progress-bar-striped progress-bar-animate active op7"
     data-toggle="tooltip" 
     title="Parts réservées&nbsp;:&nbsp;{{ '{{ gift.total_reserved}}' }}&nbsp;/&nbsp;{{ '{{ gift.number_of_parts }}' }}"
     data-bar="reserved">
</div>

对于这个指令:

app.directive('progressBarD', function() {
return {
    restrict: 'E',
    templateUrl: rootUrl + '/directive/progressBar',
    scope: {
        gift: '=gift'
    },
    link: function ($scope, $elem, attrs) {
        var bars = $elem.find(".progress-bar");
        bars.each(function() {
            var bar = $(this);
            var percent = (bar.context.dataset.bar === "gived")
                ? $scope.gift.percent_done
                : $scope.gift.percent_reserved;
            if (($().appear) && isMobileDevice === false && (bars.hasClass("no-anim") === false) ) {
                bar.appear(function () {
                    bar.addClass("progress-bar-animate").css("width", percent + "%");
                }, {accY: -150} );
            } else {
                bar.css("width", percent + "%");
            }
        });
    }
};
});

下面是指令的调用:

<progress-bar-d gift="gift"></progress-bar-d>

在执行过程中,我得到了这个错误:

Error: [$compile:ctreq] Controller 'progress', required by directive 'bar', can't be found!
http://errors.angularjs.org/1.3.15/$compile/ctreq?p0=progress&p1=bar
at angular.js:63
at getControllers (angular.js:7583)
at nodeLinkFn (angular.js:7772)
at angular.js:7998
at processQueue (angular.js:13248)
at angular.js:13264
at Scope.$get.Scope.$eval (angular.js:14466)
at Scope.$get.Scope.$digest (angular.js:14282)
at Scope.scopePrototype.$digest (hint.js:1468)
at Scope.$get.Scope.$apply (angular.js:14571)

在屏幕上,链接功能看起来可以工作,但在结束时,里面的第一个礼物消失了。

你能解释一下为什么我有这个角度误差吗?删除span也是同样的问题吗?

我终于找到问题了。

在angular指令中,我修改了bar.context.dataset.bar,它就工作了。我不知道为什么没有很好地解释,但现在它工作了。