简体   繁体   English

AngularJS错误:指令“ drawing”所需的控制器“ drawing”找不到

[英]AngularJS Error: Controller 'drawing', required by directive 'drawing', can't be found

I'm getting the above error. 我收到上述错误。 I need help fixing it. 我需要帮助修复它。 I've looked at this question and it doesn't seem to help me at all I have the required directive on the same element as the directive requiring it. 我已经看了这个问题 ,它似乎完全没有帮助我,我在与要求它的指令相同的元素上具有必需的指令。 I also don't see why my error says Controller 'drawing', required by directive 'drawing', can't be found. 我也看不到为什么我的错误提示找不到指令'drawing'所需的Controller'drawing'。 Shouldn't it say that the 'drawing' controller is required by directive 'canvasSizer'? 它不应该说指令'canvasSizer'需要'drawing'控制器吗?

Here is my code simplified for the full code see the plnkr. 这是我的完整代码的简化代码,请参见plnkr。

angular.module("drawingDirective", [])
.directive("drawing", function(){
  return {
    restrict: "A",
    scope: {},
    link: function(scope, element){
      var canvas = element[0];
      var ctx = canvas.getContext('2d');
      var $canvas = $(canvas);

      this.resize = function () {
        // save the canvas content as imageURL
        var data=canvas.toDataURL();

        // resize the canvas
        canvas.width*= window.innerWidth;
        canvas.height*= window.innerHeight;

        // scale and redraw the canvas content
        var img=new Image();
        img.onload=function(){
            ctx.drawImage(img,0,0,img.width,img.height,0,0,canvas.width,canvas.height);
        }

      };

    }
  };
})
.directive("canvasSizer", function () {
  return {
    restrict: "A",
    require: "drawing",
    link: function(scope, element, drawingCtrl){

    //first call of tellAngular when the dom is loaded
    document.addEventListener("DOMContentLoaded", drawingCtrl.resize, false);

    //calling tellAngular on resize event
    window.onresize = drawingCtrl.resize;

    }
  };
});

My html bootstraps the app and has the following div inside it. 我的html引导应用程序并在其中包含以下div。

<canvas drawing canvas-sizer></canvas>

Here is the plunker I've been working on. 这是我一直在努力的the子

Working (I think) fork of your plunkr: http://plnkr.co/edit/sEZCyyC8dZOTo2LWcdc9?p=preview 您的plunkr的工作(我认为)分支: http ://plnkr.co/edit/sEZCyyC8dZOTo2LWcdc9?p=preview

Changed a few things around, moved most of the link stuff into the controller to support exposing it to the other directive. 进行了一些更改,将大多数链接内容移到了控制器中,以支持将其暴露给其他指令。 Changed your scaling function a tiny bit (it was doing *= for the new canvas size?) 稍微更改了缩放功能(对于新的画布大小,它正在做* =吗?)

This was missing too: 这也丢失了:

img.src=data;

Check it out, hope that helps! 检查一下,希望对您有所帮助!

As Sunil commented, the simple answer is that when you require a directive, you ask for its controller to be passed into your linking function as the fourth argument (not the third, as in your code). 正如Sunil所说,简单的答案是,当您require指令时,您要求将其控制器作为第四个参数(而不是代码中的第三个)传递到链接函数中。

Since you want to use the resize property on drawingCtrl, you should give your drawing directive a controller and move this functionality there. 由于要在drawingCtrl上使用resize属性,因此应为图形指令提供一个控制器,并将此功能移到该控制器上。 See "Creating directives that communicate" here: https://docs.angularjs.org/guide/directiveAs 请参阅此处的“创建通信指令”: https : //docs.angularjs.org/guide/directiveAs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到指令“ loginForm”所需的控制器“ alertForm”! 在angularjs中 - Controller 'alertForm', required by directive 'loginForm', can't be found! in angularjs 无法找到指令&#39;ngSwitchWhen&#39;所需的AngularJS控制器&#39;ngSwitch&#39; - AngularJS Controller 'ngSwitch', required by directive 'ngSwitchWhen', can't be found Angularjs:错误:[$compile:ctreq] 指令“ngTransclude”所需的控制器“carousel”,无法找到 - Angularjs: Error: [$compile:ctreq] Controller 'carousel', required by directive 'ngTransclude', can't be found 错误:[$ compile:ctreq]指令所需的控制器,找不到 - Error: [$compile:ctreq] Controller required by directive , can't be found Angular JS缺少必需的控制器错误:找不到指令所需的控制器“ ngModel” - Angular JS Missing Required Controller error: Controller 'ngModel', required by directive can't be found Angular js错误:找不到指令yyyy所需的[$ compile:ctreq]控制器xxxx - Angular js Error: [$compile:ctreq] Controller xxxx, required by directive yyyy, can't be found 包装时找不到指令“ uibSlide”所需的控制器“ uibCarousel” - Controller 'uibCarousel', required by directive 'uibSlide', can't be found when wrapping 无法找到指令'ngTransclude'所需的控制器'carousel' - Controller 'carousel', required by directive 'ngTransclude', can't be found Contidional 模板 - 无法找到指令“mdRadioButton”所需的控制器“mdRadioGroup” - Contidional template - Controller 'mdRadioGroup', required by directive 'mdRadioButton', can't be found AngularJS指令到指令通信抛出未找到有关控制器的错误 - AngularJS directive to directive communication throwing an error about controller not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM