Angular 2 Modal作为自己的组件 - 共享数据

时间:2016-11-25 12:38:25

标签: angular components

我对angular2很新,我有一个问题。 我正在编写一个应用来管理电视服务器。 在应用程序中我有不同的组件,所有这些组件都有一个视图!

现在我想要一个组件'newTimerComponent',它是一个自己的组件,但是其他组件的'子'。 这个组件“只是”一个模态。

我的目标: 如果我点击任何其他组件中的按钮,'newTimerComponent'将打开模态,我可以在模态中执行某些操作。 此外,我需要在组件和'newTimerComponent'之间共享一些数据,但它只是一个变量和'newTimerComponent'的返回值。

到目前为止,我的代码来自'NewTimerComponent':

@Component({
    selector: 'timer',
    moduleId: module.id,
    templateUrl: 'timer.component.html'
})


export class TimerComponent implements OnInit {

    private selectedTimer;
    private newTimer: boolean;

    constructor(private router: Router, private variables: Variables, private apiService: ApiService, private formatter: Formatter, private helperService: HelperService) {
        if (!this.variables.getIsLoggedIn()) {
            this.router.navigate(['login']);
        }
    }

    ngOnInit() {

    }

}

此组件的HTML:

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
        aria-labelledby="myModalLabel" *ngIf="selectedTimer">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h3 class="modal-title" *ngIf="newTimer">Header</h3>
                </div>
                <div class="modal-body">
// deleted everything else
            </div>
        </div>
    </div>

我该怎么做,以便我可以在任何其他组件中使用此组件?

非常感谢:)

0 个答案:

没有答案
相关问题