我想编写一个在构造函数中获取“前缀”参数的TypeScript类,该类还需要访问LogService注入。
使用纯JavaScript,您应该这样做:
angular.module('myModule', []).factory('LogWithPrefixFactory', ['LogService', function(LogService) {
var LogWithPrefixFactory = function(prefix) {
this.prefix = prefix;
}
LogWithPrefixFactory.prototype.log = function(txt) {
// we have access to the injected LogService
LogService.log(this.prefix, txt);
}
return LogWithPrefixFactory;
}]);
因此,当您将此工厂注入控制器时,可以像这样多次启动它(无需注入LogService):
angular.module('myModule').controller('Ctrl', function(LogWithPrefixFactory) {
var foo = new LogWithPrefixFactory("My PREFIX");
var foo = new LogWithPrefixFactory("My OTHER PREFIX");
}
您将如何在TypeScript类中定义此Factory?无法在函数内部定义TypeScript类。此类应有权访问LogService,但不能通过注入之一获取它。
至少有2个选项。
第一个选择是,LogWithPrefixFactory
提供一种getInstance
返回前缀记录器的方法。
module services {
class LogService {
$window: any;
constructor($window: any) {
this.$window = $window;
}
log(prefix: string, txt: string) {
this.$window.alert(prefix + ' :: ' + txt);
}
}
angular.module('services').service('LogService', ['$window', LogService]);
export interface ILog {
log: (txt) => void;
}
export class LogWithPrefixFactory {
logService: LogService;
constructor(logService: LogService) {
this.logService = logService;
}
getInstance(prefix: string): ILog {
return {
log: (txt: string) => this.logService.log(prefix, txt);
}
}
}
angular.module('services').service('LogWithPrefixFactory', ['LogService', services.LogWithPrefixFactory]);
}
可以在控制器中使用,例如:
this.log1 = logWithPrefixFactory.getInstance("prefix1");
this.log2 = logWithPrefixFactory.getInstance("prefix2");
在这里完整地塞一下。
第二个选项(类似于另一个答案),为Angular提供了另一个要用作构造函数的函数,该函数手动处理LogService
构造函数注入(个人而言,我不喜欢static
)。
angular.module('services').service('LogWithPrefixFactory', ['LogService', function(logService) {
return function LogWithPrefixFactory(prefix) {
return new LogWithPrefix(prefix, logService);
};
}]);
可以在控制器中使用,例如:
this.log1 = new LogWithPrefixFactory("prefix1");
this.log2 = new LogWithPrefixFactory("prefix2");
甚至:
this.log1 = LogWithPrefixFactory("prefix1");
this.log2 = LogWithPrefixFactory("prefix2");
LogWithPrefixFactory
被注入到控制器中,但不是TypeScript类的构造函数,它是中间函数,在“手动”注入后,该函数返回该类的实际实例LogService
。
在这里完整地塞一下。
注意:这些插件会在浏览器上同步编译打字稿。我只在Chrome上测试过。不能保证它们会起作用。最后,我手动添加了一小部分angular.d.ts。完整文件非常大,我的代理服务器不允许大型POST。
问题内容: 嘿 语言是Java。我想扩展构造函数具有参数的此类。 这是主要的课程 这是儿童班 如何编写正确的语法?并且错误是“构造函数无法应用于给定类型” 问题答案: 您可以定义构造函数所需的任何参数,但是有必要将超类的一个构造函数作为自己的构造函数的第一行来调用。可以使用或来完成。
我试图构建一个包含getter和setter的构造函数,除了在参数中调用它之外,这些构造函数似乎对我的所有属性都很好。 当我读到这行代码时: 然后在控制台中出现以下错误: 构造函数flip(String)未定义构造函数flip(String)未定义 也许我错过了什么?
让我们分析下面的代码片段: 以上代码不会引发任何错误。我希望它会抛出构造函数调用,在这里我传递5。 似乎构造函数的参数被推断为,其中,静态地很容易发现我将其分配给。 问题是:TypeScript在这里允许,或者换言之,被推断为,而在这种情况下,很明显它必须是一个字符串,这有什么特殊的原因吗? 我知道我可以做另一个这样的类定义: 但这里我指定了参数类型,所以这里没有推断。我也可以这样做: 也不会有任
我是新的Android和Java,并试图使基于位置的应用程序。 编辑 我做了一个非常非常简单的测试代码,得到了同样的错误。这是java: 我也犯了同样的错误。以下是我的应用程序级构建中的依赖项。格雷德尔: 原帖 我试图使用ViewModel和LiveData来更新用户位置,因为我知道这是生命周期感知的最佳方式。我有一个默认的地图活动... 一个扩展LiveData以存储用户位置的类。。。 以及一个
鉴于 IntelliJ IDEA 2016.1.1报告“可以替换为方法引用”。 我知道无参数构造函数的< code>Foo::new语法,但是不知道如何将< code>foo作为参数传入。我肯定漏掉了什么。
我有一个类,它有一些属性/方法,返回最初通过构造函数传递的值: 人们建议用泛型作为打字稿的等价物: 现在我有了一个子类,它调用超类的构造函数并传递以下选项: TypeScript编译器对此不满意,希望通过类型扩展Base类。但是我有两次信息,在超级呼叫和扩展符号中。什么是正确的ts语法来解决我的问题? (键入脚本中的代码)