typescript + angular: factory with dependencies error
typescript + angular: factory with dependencies error
我要把我的angular应用迁移到typescript。我有以下代码:
/// <reference path="../../../typings/tsd.d.ts" />
class UiRoutingHelper {
constructor(public $state: any) {};
reloadCurrentView() {
return this.$state.go(this.$state.current.name, this.$state.params, {
reload: true
});
};
}
mp.core.CoreModule.factory('UiRoutingHelper', ['$state', UiRoutingHelper]);
可以正确编译。整个应用程序构建过程也会正确构建。但在运行时,我得到以下错误:
Error: [$injector:undef] Provider 'UiRoutingHelper' must return a value from $get factory method.
http://errors.angularjs.org/1.3.15/$injector/undef?p0=UiRoutingHelper
at angular.js:63
at Object.enforcedReturnValue [as $get] (angular.js:4058)
at Object.invoke (angular.js:4203)
at angular.js:4021
at getService (angular.js:4162)
at Object.invoke (angular.js:4194)
at extend.instance (angular.js:8493)
at angular.js:7739
at forEach (angular.js:331)
at nodeLinkFn (angular.js:7738)
请告诉我我做错了什么。
typings//tsd.d.ts
只是一个引导文件,它加载了一些预定义的接口。mp.core.CoreModule
之前已经被声明过了(TypeScript中也有)。
不要使用factory
类。只要用service
。代码:
class UiRoutingHelper {
constructor(public $state: any) {};
reloadCurrentView() {
return this.$state.go(this.$state.current.name, this.$state.params, {
reload: true
});
};
}
mp.core.CoreModule.service('UiRoutingHelper', ['$state', UiRoutingHelper]);
这里有一个关于这个话题的视频:https://www.youtube.com/watch?v=Yis8m3BdnEM
在Angular中使用Typescript时,工厂注册需要一个返回新工厂实例的函数。
你可以这样做:
class UiRoutingHelper {
static $inject = ['$state'];
constructor(public $state: any) {};
reloadCurrentView() {
return this.$state.go(this.$state.current.name, this.$state.params, {
reload: true
});
};
}
function factory(){
return new UiRoutingHelper();
}
mp.core.CoreModule.factory('UiRoutingHelper', factory);
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- Understanding Javascript scope with "var that = this&qu
- Architecture for CPU intensive tasks with NodeJS & Socke
- How to declare a Map containing certain properties with flow
- 设置'这'在React with Inverse Data Flow中
- Unit-testing multiple use cases with Karma & Mocha.js
- Modify Javascript with C#
- Circular Dependencies with RxJS. 对孢子进行建模
- typescript + angular: factory with dependencies error
- 在RequireJS中对define with dependencies感到困惑