[英]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>
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.