Typescript将数据从父控制器传递到子控制器

时间:2016-05-23 11:32:21

标签: typescript controller

我刚开始使用打字稿。我需要将数据(this.level,this.userData)从父控制器传递到子控制器。在angularjs中,我们将创建服务,但如何在typescript

中实现这一点

家长控制器:

    export class ParentController {
        static $inject: string[] = ['parentService', '$q', 'httpSharedService','$mdDialog'];
        private level: IDataModel[];
        private userData: any;
        private UserRole: boolean = false;
        private hasNext: boolean = false;
        private hasPrev: boolean = true;
        private clickCount: number = 1;
        private selectedCard:number = -1;

        constructor(private parentService:parentService ,private $q:ng.IQService,
            private httpSharedService: httpSharedService, private $mdDialog: ng.material.IDialogService) {
            this.$q.all([        
                this.httpSharedService.useGetHandler('../../../../mockdata/level.json',''),
                this.httpSharedService.useGetHandler('../../../../mockdata/getUserInfo.json','')
            ]).then((params:any) =>{
                this.level = params[0]; // this data to be passed to child controller
                this.userData = params[1]; // this data to be passed to child controller
                if(this.userData.role == 'Power User' && this.userData.type == 'Customer'){
                    this.UserRole = true;
                }
            });
        }

        addDivision(ev: Event) {
            this.$mdDialog.show({
                clickOutsideToClose: false,
                template:  require('./child.element.html'),
                parent: angular.element(document.body),
                hasBackdrop: true,
                controller: ChildController,
                controllerAs: "popel"
            })
        }


    }

儿童控制器:

    export class ChildController {
        static $inject: any = ['$mdDialog', 'httpSharedService'];  
        public divname: any; 
        public showErrors: boolean = false;

        constructor(private $mdDialog: ng.material.IDialogService, private httpSharedService: httpSharedService) {

        }

        save(){
            this.showErrors = true;
            alert(this.divname);
        }

         closeDialog(): void {
            this.$mdDialog.cancel();
        }
    }

0 个答案:

没有答案
相关问题