您好我需要您帮助将这个优秀的javascript代码转换为类型化的ui-router。
问题1:javascript回调是否等同于typescript类实例的返回?如果我没有朝着正确的方向前进,请随时帮助我。
上面我的意思的例子: 在下面的代码中,我认为提供者意味着回调。 的JavaScript
var provider = this;
this.$get = function() {
return provider;
}
我想但我不确定将打字稿翻译为:
private _Service:any;
public $get() { return this._Service; }
constructor(private $stateProvider:any ) {
this._Service = this;
}
这是我想要转换的内容
.provider('modalState', function($stateProvider) {
var provider = this;
this.$get = function() {
return provider;
}
this.state = function(stateName, options) {
var modalInstance;
$stateProvider.state(stateName, {
url: options.url,
onEnter: function($modal, $state) {
modalInstance = $modal.open(options);
modalInstance.result['finally'](function() {
modalInstance = null;
if ($state.$current.name === stateName) {
$state.go('^');
}
});
},
onExit: function() {
if (modalInstance) {
modalInstance.close();
}
}
});
};
})
这是一个很好的代码:Using ui-router with Bootstrap-ui modal
这里我尝试将其转换为typescript
class ModalService {
private _Service:any;
public $get() { return this._Service; }
constructor(private $stateProvider:any ) {
this._Service = this;
}
state(stateName:string, options:any ) : void {
this.state = (stateName, options)=> {
var modalInstance;
this.$stateProvider.state(stateName, {
url: options.url,
onEnter: ($modal, $state) => {
modalInstance = $modal.open(options);
modalInstance.result['finally']( ()=> {
modalInstance = null;
if ($state.$current.name === stateName) {
$state.go('^');
}
});
},
onExit: () => {
if (modalInstance) {
modalInstance.close();
}
}
});
};
}
}
答案 0 :(得分:0)
结果写出上面这样的函数解决问题,模态现在正在工作。
public $get() {
return () => { return ModalService; }
}
constructor(private $stateProvider) {}
经过几个小时的思考后,这是相同的。很简单,但我被困在上面。希望它能帮助其他人。