简体   繁体   English

是否可以将jQuery对象作为构造函数参数传递给typescript对象?

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

相关问题 无法将 object 作为构造函数中的参数传递 - Unable to pass the object as parameter in constructor TypeScript-将对象文字传递给类构造函数 - TypeScript - Pass object literal to class constructor 在保存上下文的同时将对象作为构造函数的参数传递 - Pass object as parameter for constructor among with saving the context 如何将 object 作为 function 的参数传递给 TypeScript 中可选的 object 字段? - How to pass object as a parameter for function with optional object fileds in TypeScript? 如何通过传递 object 作为参数来调用 javascript/typescript 构造函数? - How can I call a javascript/typescript constructor by passing an object as parameter? 如何在Javascript / jQuery中将对象/模板作为参数传递 - How to pass object/template as parameter in Javascript/jQuery 在函数参数中传递对象以在jquery中克隆 - pass object in function parameter to clone in jquery javascript,jquery,plupload是否可以在不重新加载页面/对象的情况下将参数传递给URL - javascript, jquery, plupload is it possible to pass a parameter to the URL on the fly without reloading the page/object 对象声明的typescript参数 - typescript parameter to an object declaration 在构造函数中通过构造函数传递对象? - pass object on contructor in typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM