저는 많은 구성 요소가있는 전형적인 앵귤러 2 응용 프로그램이 있습니다.
이 모달 구성 요소를 설치했습니다 : https://github.com/pleerock/ng2-modal.
더 많은 구성 요소간에 동일한 모달을 공유 할 수있는 방법이 있습니까? 나는 더 잘 설명한다. 다른 구성 요소의 다른 버튼을 클릭 할 때 동일한 모달을 열어야합니다. 가능한가? 구성 요소간에 각도 2 모달을 공유합니다.
내 app.ts 파일 아래에 여기에 게시 :
21/12/2016 갱신
이 @echonax 제안에 따라, 나는 나의 쿵하는 소리를 업데이트 :
//our root app component
import {
NgModule,
ComponentRef,
Injectable,
Component,
Injector,
ViewContainerRef,
ViewChild, ComponentFactoryResolver} from "@angular/core";
import {BrowserModule} from '@angular/platform-browser'
import {Subject} from 'rxjs/Subject';
@Injectable()
export class SharedService {
showModal:Subject<any> = new Subject();
}
@Component({
selector: 'child-component',
template: `
<button (click)="showDialog()">show modal from child</button>
`,
})
export class ChildComponent {
constructor(private sharedService:SharedService) {}
showDialog() {
this.sharedService.showModal.next(CompComponent);
}
}
@Component({
selector: 'comp-comp',
template: `MyComponent`
})
export class CompComponent { }
@Component({
selector: 'modal-comp',
template: `
<div class="modal fade" id="theModal" tabindex="-1" role="dialog" aria-labelledby="theModalLabel">
<div class="modal-dialog largeWidth" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="theModalLabel">The Label</h4></div>
<div class="modal-body" #theBody>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" (close)="close()">Close</button>
</div></div></div></div>
`
})
export class ModalComponent {
@ViewChild('theBody', {read: ViewContainerRef}) theBody;
cmp:ComponentRef<any>;
cmpRef:ComponentRef<any>;
constructor(
sharedService:SharedService,
private componentFactoryResolver: ComponentFactoryResolver,
injector: Injector) {
sharedService.showModal.subscribe(type => {
if(this.cmp) {
this.cmp.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(type);
this.cmpRef = this.theBody.createComponent(factory)
$('#theModal').modal('show');
});
}
close() {
if(this.cmp) {
this.cmp.destroy();
}
this.cmp = null;
}
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello</h2>
<button (click)="showDialog()">show modal</button>
<child-component></child-component>
</div>
`,
})
export class App {
constructor(private sharedService:SharedService) {}
showDialog() {
this.sharedService.showModal.next(CompComponent);
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App, ModalComponent, CompComponent, ChildComponent],
providers: [SharedService],
entryComponents: [CompComponent],
bootstrap: [ App, ModalComponent ]
})
export class AppModule{}
스테이 튜닝!
당신은 무엇을 시도? 이를 참조하고 작동하지 않는 코드를 게시하여 도움을 받으십시오. 사람들은 당신이 무언가를 시도하고 누군가에게 당신을 대신해달라고 요청하기보다는 도움이 필요할 때 더 많은 도움을 줄 것입니다. –
알았어. 어쨌든, 내가 시도한 접근 방식 중 어떤 것이 든 필요한 부분에 적합하기 때문에 게시하는 것이 유용하다고 생각하지 않았습니다. 나는 어제 오후 전체를 시도했다. 그것은 게으름이 아니었다. –
그 plunker의 원래 버전은 실제로 내 질문입니다 : http://stackoverflow.com/questions/36566698/how-to-dynamically-create-bootstrap-modals-as-angular2-components 및 @ Günter 내가 생각하는 놀라운 대답을 만들었습니다 정말 과소 평가됐다. 작은 실수가 있지만 Günter에게 알려 주겠다. https://plnkr.co/edit/oMCZIJq58WdLgYf2D0Di?p=preview – echonax