Javascript 通过嵌套组件调用父组件函数
我的角度代码有问题。我想从由Javascript 通过嵌套组件调用父组件函数,javascript,angular,components,Javascript,Angular,Components,我的角度代码有问题。我想从由component()Angular函数创建的嵌套组件调用父组件的函数 请参阅下面我的代码: import { Compiler, Component, ComponentRef, Injector, NgModule, NgModuleRef, ViewChild, ViewContainerRef } from '@angular/core'; import { ActivatedRoute } from "@angular/router"; import
component()
Angular函数创建的嵌套组件调用父组件的函数
请参阅下面我的代码:
import {
Compiler, Component, ComponentRef, Injector, NgModule, NgModuleRef, ViewChild, ViewContainerRef
} from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { FormsModule, NgForm } from "@angular/forms";
import { HttpService } from './ht.service';
@Component({
selector: 'app-module-routing',
templateUrl: './module-routing.component.html'
})
export class ModuleRoutingComponent {
@ViewChild('vc', {read: ViewContainerRef}) vc: ViewContainerRef;
private cmpRef: ComponentRef<any>;
private module_name: string;
private module_link: any;
private module_links = [
{"access": "salessalquoent", "link": "sales\/sales_order_entry.php?NewQuotation=Yes"},
];
constructor(private compiler: Compiler,
private injector: Injector,
private m: NgModuleRef<any>,
private route: ActivatedRoute,
private httpService: HttpService) {}
test() { //<-- Want to Call this function
alert('function called successfully');
}
printPage(template, httpService, link) {
const styles = [`input { width: 100px, display: block }`];
const tmpCmp = Component({ template, styles }) (
class {
OnSubmit(form: NgForm) {
console.log(form);
this.ModuleRoutingComponent.test(); //<-- Want to call Parent Component function here
httpService.getPage(link, form.value)
.subscribe(
data => this.instance.printPage(data, httpService, link)
);
return false;
};
}
);
const tmpModule = NgModule({
imports: [FormsModule],
declarations: [tmpCmp]
})(class {});
this.compiler.compileModuleAndAllComponentsAsync(tmpModule)
.then((factories) => {
const f = factories.componentFactories[0];
this.cmpRef = f.create(this.injector, [], null, this.m);
this.cmpRef.instance.name = 'someForm';
this.vc.insert(this.cmpRef.hostView);
});
}
}
导入{
编译器、组件、组件引用、注入器、NgModule、NgModuleRef、ViewChild、ViewContainerRef
}从“@angular/core”开始;
从“@angular/router”导入{ActivatedRoute}”;
从“@angular/forms”导入{FormsModule,NgForm}”;
从“/ht.service”导入{HttpService};
@组成部分({
选择器:“应用程序模块路由”,
templateUrl:“./module routing.component.html”
})
导出类模块OutingComponent{
@ViewChild('vc',{read:ViewContainerRef})vc:ViewContainerRef;
私有cmpRef:ComponentRef;
私有模块名称:字符串;
专用模块链接:任意;
专用模块链接=[
{“access”:“salessalquoent”,“link”:“sales\/sales\u order\u entry.php?newquote=Yes”},
];
构造函数(私有编译器:编译器,
专用注射器:注射器,
私人m:NgModuleRef,
专用路由:激活的路由,
私有httpService:httpService){}
test(){//{
常数f=工厂。组件工厂[0];
this.cmpRef=f.create(this.injector,[],null,this.m);
this.cmpRef.instance.name='someForm';
this.vc.insert(this.cmpRef.hostView);
});
}
}
请查看我在代码中的注释,您将看到我的实际意思:
//<-- Want to Call this function
//我试图在父组件中处理onsubmit,但它不起作用。如果有任何方法可以使它在父组件中工作,而html在子组件中解析,那么这可能是更好的解决方案。若有任何方法可以使它在父组件中工作,而在子组件中解析html,那个么这可能是更好的解决方案。
//<-- Want to call Parent Component function here