Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何将服务注入函数?_Angular_Typescript - Fatal编程技术网

Angular 如何将服务注入函数?

Angular 如何将服务注入函数?,angular,typescript,Angular,Typescript,Reflect是否提供了这样的方法,以便获取函数的params类型?我怎样才能得到服务类的单身 场景是,我将使用一个实现下面接口的对象来呈现表单视图,我有一些自定义服务,这些服务应该在运行时注入$validator、$parser和$formatter var someObject = { func: (http: Http)=>{ // do something } } --------------------------------------------- //

Reflect是否提供了这样的方法,以便获取函数的params类型?我怎样才能得到服务类的单身

场景是,我将使用一个实现下面接口的对象来呈现表单视图,我有一些自定义服务,这些服务应该在运行时注入$validator、$parser和$formatter

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;
}

我在我的项目中也遇到了同样的问题。我们使用的是角度4+。 下面是我如何解决它的:

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
    }
  ]
}

不。Angular DI只注入类的构造函数。那么有没有办法通过Reflect获取函数的参数类型?对不起,我不知道。你为什么不把它变成一个类,让Angular2来完成这项工作呢?问题是我正试图使用一个模板组件来降低构建新表单组件的复杂性。听起来很奇怪。为什么
$validator
$parser
$formatter
必须是函数而不是
IViewSchema
的方法。如果将服务注入到
ViewSchemaImpl
中,则所有方法都可以访问服务(
ViewSchemaImpl
的属性)。
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;
};