[英]Passing Value to Functions From ngOnInit
I have a button that calls clip function. 我有一个调用剪辑功能的按钮。 I need to pass values from ngOnInit to that function but i can't.
我需要将值从ngOnInit传递给该函数,但我不能。 I get undefined error for (for example) this.firstX .
我收到this.firstX的未定义错误。 How can i pass values from ngOnInit to functions?
如何将ngOnInit中的值传递给函数?
export class AppAppComponent implements OnInit {
public firstX: number;
public firstY: number;
public lastX: number;
public lastY: number;
constructor(mdIconRegistry: MdIconRegistry) {
mdIconRegistry
.registerFontClassAlias('fontawesome', 'fa');
}
ngOnInit() {
let canvas = new fabric.Canvas('cLeft');
let src = "../../images/kupurSecond.jpg";
canvas.on('mouse:down', function (event) {
var position = canvas.getPointer(event.e);
this.firstX = position.x;
this.firstY = position.y;
});
canvas.on('mouse:up', function (event) {
var position = canvas.getPointer(event.e);
this.lastX = position.x;
this.lastY = position.y;
// ReLoading Image
loadImage(src, canvas, this.firstX, this.firstY, this.lastX, this.lastY);
});
loadImage(src, canvas);
};
public clip() {
console.log(this.firstX);
}
}
You have to use the arrow notation to remain the same this
context. 您必须使用箭头表示法在
this
上下文中保持相同。
canvas.on('mouse:down', (event) => {
var position = canvas.getPointer(event.e);
this.firstX = position.x;
this.firstY = position.y;
});
Same goes for the mouse:up
function. mouse:up
函数也是如此。 Just never use the word function
in typescript, and you are golden 只是永远不要在打字稿中使用单词
function
,您真是太棒了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.