[英]Is it possible to pass a jQuery object as a constructor parameter to a typescript object?
We are trying to figure out a peculiar behaviour of typescript. 我们试图弄清楚打字稿的特殊行为。 In the following two code examples, a jQuery object should be altered by typescript.
在以下两个代码示例中,jQuery对象应该由typescript更改。 In the example that works fine, we reference the jQuery object by using the selector engine in a method after our typescript class is instantiated.
在工作正常的示例中,我们在实例化typescript类之后通过在方法中使用选择器引擎来引用jQuery对象。 This works:
这有效:
namespace Company.Module {
export class GenericService {
private statefulObject: JQuery;
public constructor( ) {
}
private _toggleObjectState(): void {
this._statefulObject.toggleClass('text-hide text-success');
}
public SetObjectState(): void {
this.statefulObject = $('#statefulObject');
_toggleObjectState();
}
}
}
Whereas passing the jQuery object in the constructor does not work: 而在构造函数中传递jQuery对象不起作用:
namespace Company.Module {
export class GenericService {
private statefulObject: JQuery;
public constructor( _statefulObject: JQuery ) {
this.statefulObject = _statefulObject;
}
private _toggleObjectState(): void {
this._statefulObject.toggleClass('text-hide text-success');
}
public SetObjectState(): void {
_toggleObjectState();
}
}
}
We would like to understand why this is the case. 我们想了解为什么会这样。
Unfortunately this was a non-issue. 不幸的是,这不是问题。 Our code did not work because we were referencing an object that was added to the DOM dynamically.
我们的代码不起作用,因为我们引用了一个动态添加到DOM的对象。 When passed to the
typescript
constructor, the object simply was not loaded in the DOM, therefore it could not be manipulated. 当传递给
typescript
构造函数时,该对象根本没有加载到DOM中,因此无法对其进行操作。 Moderators please feel free to remove this question. 主持人请随时删除此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.