2016-09-30 68 views
0
var someObject = { 
    func: (http: Http)=>{ 
    // do something 
    } 
} 

--------------------------------------------- 

// somewhere in an ng2 module 
function injectServices(theObject){ 

    let services = getServices(theObject.func) 

    //inject services into theObject 
    .... 

} 

// return services 
function getServices(func: Function){ 
    // get params' type of func 
    let paramTypes = ... 
    // get services' singleton 
    let singletons = ... 

    return singletons; 
} 

确实反映提供了这样的方法,这样我可以得到PARAMS'类型的函数吗?我怎么能得到服务类的单身人士?我怎样才能将服务注入功能?

的情况是,我会采取它实现下面的界面来呈现表单视图的对象,我应该在运行时注入到$验证,$解析器和$格式化一些定制的服务。

interface IViewSchema { 
    "title": String, 
    "buttons": [ 
    { 
     "buttonText": String, 
     "role?": "cancel" | "submit", 
     "callback?": 'cancel' | 'submit' | Function, 
     "disabled": String 
    } 
    ], 
    "viewTemplate?": String, 
    "formControls?": [ 
    { 
     "formTemplate?": String, 
     "label": String, 
     "maxLength?": number, // max length of input 
     "minLength?": number, // min length of input 
     "hidden?": String, // if hidden is true, hide the form control 
     "disabled?": String, // if data is editable or not, default true, 
     "placeholder?": String, 
     "model?": String, 
     "$validate?":Function[], 
     "$parser?": { // parse modal data to view data 
     "async?": boolean, // default false, run async parser or not 
     "remote?": { // enabled only when async is true 
      "url": String, // remote url 
      "method?": String, // calling method 
      "headers?": JSON, 
      "body?": String | JSON 
     }, 
     "parse?": Function 
     }, 
     "$formatter?": Function 
    } 
    ] 
} 
+1

号角DI注入到只类的构造函数。 –

+0

然后有没有什么办法可以通过反射??函数的参数类型? –

+0

对不起,我不知道。你为什么不把它变成一堂课,让Angular2做这个工作? –

回答

0

我在我的项目上面临同样的问题。我们正在使用Angular 4 + Redux。 这是我如何解决这个问题:

import { AppActions } from '../app.actions'; 
import { IAction } from './model.interface'; 
import { SomeService} from '../services/some.service'; 
const someService = new SomeService(); 
export function someReducer(reducersKey) { 
    return function (state: IState = INITIAL_STATE, 
        action: IAction): IState { 
if (action.type === AppActions.REHYDRATE) { 
    if (action.payload) { 
const newState = someService.awesomeMethod(); 
return {...state, ...newState}; 
    } 
    return state; 
} 
return state; 
};