2017-02-21 29 views
3

所以我在Angular2中有兩個組件。當用戶單擊component1中的按鈕時,我有一種方法將sharedservice中的數據存儲到變量中。該變量在組件2 ngOnInit()中訪問。然而,由於它不等待點擊事件,所以初始化爲ngOnInit()中的變量未定義。如何等待angular2中的用戶點擊事件?

總而言之,我該如何讓angular2 component2等待component1中的單擊事件?

在JavaScript中,我們可以使用點擊事件偵聽器輕鬆地做到這一點,或者有一個回調函數,但我不知道如何在角度上實現相同的想法。

請您分享您的想法。

這是我到目前爲止有:

modules.component.html

<tr *ngFor="let module of modules" #moduleObject> 
    <a class="test" (click)="module._clicked = !module._clicked"></a> 
    <ul class="dropdown-menu" role="menu" [ngStyle]="{'display': module._clicked ? 'block' : 'none'}" > 
    <li><a (click)="_showDialogue =! _showDialogue; _getModuleCode(module)"><img src="../../assets/img/pencil.svg" alt="" width="13px">Edit Module</a></li> 

    </ul> 
</tr> 

我會從modules.component.ts刪除不必要的代碼,因爲它只會吹在這裏 modules.component.ts一切

import {SharedService} from "../shared/shared.service"; 
import {DepartmentsService} from "./departments.service"; 
import {ActivatedRoute, Params} from "@angular/router"; 
import {Module} from "../dashboard/Module"; 

@Component({ 
    selector: 'app-modules', 
    templateUrl: './modules.component.html', 
    providers: [DepartmentsService] 
}) 
export class ModulesComponent implements OnInit { 
    service; 

    modules: Module[]; 

    constructor(
    private activatedRoute: ActivatedRoute, 
    service : SharedService, 
    private departmentsService: DepartmentsService, 
    route: ActivatedRoute) { 
    route.params.subscribe(p => {this.department = p['dname']; }); 
    this.service = service; 
    } 

    _getModuleCode(moduleObject) { 
    this.departmentsService.moduleObject = moduleObject; 
    } 


    ngOnInit() { } 
} 

departments.service.ts

// assume necessary imports above 
@Injectable() 
export class DepartmentsService { 
    public _facultyName  :string; 
    public _departmentName :string; 
    public moduleObject:Module; 
    constructor() {} 
} 

然後我打電話moduleObject變量從這個組件: edit-forms.component.html

import {Module} from "./Module"; 
import {DepartmentsService} from "../components/departments.service"; 
//Change 
@Component({ 
    selector: 'enable-module-form', 
    templateUrl: './edit-forms.component.html', 
    providers:[ModuleService] 
}) 
export class EnableModFormComponent { 
    constructor( 
    private moduleService: ModuleService, 
    private departmentService: DepartmentsService 
) { } 

    ngOnInit(){ 
    console.log(this.departmentService.moduleObject); 
    } 

} 
+0

你會使用'Observable'。你可以發佈你的場景的一些代碼,所以我們可以給你一個更現實的答案? – acdcjunior

+0

肯定的事情。請檢查我的最新編輯@acdcjunior – DingDong

+0

是否有運氣? @acdcjunior – DingDong

回答

2

這應該工作!

DepartmentService:

private moduleSource = new Subject<any>(); 
moduleValue = this.moduleSource.asObservable(); 

moduleValueReceived(module) { 
    //emit value 
    this.moduleSource.next(module) 
} 

和你的父母,當你有服務中設定的值值:

_getModuleCode(moduleObject) { 
    this.departmentsService.moduleValueReceived(moduleObject); 
} 

,並在孩子的構造,訂閱值:

constructor( 
    private moduleService: ModuleService, 
    private departmentService: DepartmentsService 
) { 
    moduleService.moduleValue.subscribe(moduleObject => { 
     console.log(moduleObject); // here you have your value 
    }) 
    } 

Here in the official docs你有與進一步解釋等價的例子比我提供的。

希望這會有所幫助!

+0

啊再次感謝你。不知道沒有你我會做什麼。真的很感激它。雖然它確實有效。但由於某些原因,訂閱(i => i.console.log(i))'多次打印clicked元素。這是爲什麼?我只想讓它返回一次clicked元素。 – DingDong

+0

因爲我有一個按鈕列表,當我點擊按鈕時,我得到正確的數據,但它根據'ul li'中的多少個按鈕返回數據我有 – DingDong

+0

好吧,如果它被多次打印,那會建議多次發生更改,因爲每當發生更改時,訂閱自然訂閱:)在您提供的代碼中沒有看到任何按鈕?你能再詳細一點嗎?所有其他的代碼對我來說似乎很清楚,但是如果你確實也有一些按鈕的話,可能會展示更多的html。所以我會更好地瞭解在視圖中發生了什麼:) – Alex