2016-03-02 122 views
13

組件如何更改另一個組件上的變量。例如:角度2更改另一個組件上的組件變量

我有一個組件app.component.ts

@Component({ 
    selector: 'my-app', 
    template: ` 
    <nav *ngIf="onMain == false"> 
     Hello 
    </nav> 
    ` 
}) 

export class AppComponent{ 
    onMain: Boolean; 

    constructor(){ 
     this.onMain = false; 
    } 
} 

我有我想在我的應用程序組件來改變onMain另一個組件main.component.ts

import {AppComponent} from '../app.component'; 

@Component({ 
    selector: 'main-app', 
    template: `` 
}) 

export class MainComponent{ 

    constructor() { 
     this.appComponent = AppComponent; 
     this.appComponent.onMain = true; 
    } 
} 

我會認爲你好就會消失,但它沒有。我如何讓一個組件更改另一個組件的值?

+0

您可以在**服務使用'EventEmitter' **。然後讓AppComponent訂閱它以獲取更改事件。 –

回答

13

首先,你有兩個組件之間沒有連接或者也可以是不正確的在你的代碼。如果您有父/子方案,則可以使用angular2的@Input,@Output。如果您沒有父/母方案,您可以使用angular2的EventEmitter,SharedService

Working demo-EventEmitter way

我已經考慮AppComponent是一個爲父級和MainComponent作爲子組件。通過使用angular2SharedService & EventEmitter概念,我可以通過單擊屬於「MainComponent's」視圖的按鈕來隱藏AppComponent's視圖的一部分。

AppComponent.ts

import {Component,bind,CORE_DIRECTIVES,OnInit} from 'angular2/core'; 
import {MainComponent} from 'src/MainComponent'; 
import {SharedService} from 'src/shared.service'; 
@Component({ 
    selector: 'my-app', 
    directives:[MainComponent], 
    template: `<h1>AppComponent {{onMain}}</h1> 
    <div *ngIf="onMain == false"> 
     Hello 
     <br> __________________________________<br> 
    </div> 

    <main-app></main-app> 
    ` 
}) 

export class AppComponent implements OnInit { 
    onMain: Boolean; 

    constructor(ss: SharedService) { 
     this.onMain = false; 
     this.ss = ss; 
    } 



    ngOnInit() { 
    this.subscription = this.ss.getEmittedValue() 
     .subscribe(item => this.onMain=item); 
    } 

} 

MainComponent.ts

import {Component,bind,CORE_DIRECTIVES} from 'angular2/core'; 
import {SharedService} from 'src/shared.service'; 
@Component({ 
    selector: 'main-app', 

    template: `<h1> MainComponent</h1> 
    <button (click)="changeName()">Change Name</button> 
    ` 
}) 

export class MainComponent { 



    constructor(ss: SharedService) { 
     this.ss = ss; 
    } 

    changeName() { 
     this.ss.change(); 
    } 
} 

shared.service.ts

import {Component, Injectable,Input,Output,EventEmitter} from 'angular2/core' 


@Injectable() 
export class SharedService { 
    @Output() fire: EventEmitter<any> = new EventEmitter(); 

    constructor() { 
    console.log('shared service started'); 
    } 

    change() { 
    console.log('change started'); 
    this.fire.emit(true); 
    } 

    getEmittedValue() { 
    return this.fire; 
    } 

} 
+0

此訂閱源來自AppComponent?我收到一個錯誤消息,AppComponent上不存在屬性訂閱 – ClickThisNick

+0

你添加了**'rx.js' **文件嗎?在我的plunker演示中查看**'index.html' **。你會發現需要的文件的必要的引用。如果答案符合您的要求,您應該接受它作爲答案,以便其他人可以將其作爲答案。 – micronyks

+0

@micronyks謝謝。你還可以分享package.json嗎?因爲事情不適用於最新的平臺和其他軟件包。 –

1

是的,你可以從一個組件這樣做 你能到達每一個方法和注射類的變量改變變量值到另一個爲此,你必須 inject組件出口類到父組件(零件)。

import {Component} from 'angular2/core'; 
@Component({ 
    selector: 'my-app', 
    templateUrl: `myTemplate.html`, 
    directive: [AppComponent2] 
}) 

export class AppComponent { 
    variable1: boolean = true; 
} 

@Component({ 
    selector: 'my-app2', 
    templateUrl: `temp2.html`, 
    providers: [AppComponent] 
}) 

export class AppComponent2 { 
    constructor(private appComponent: AppComponent){ } 
    Fun(){ 
    console.log('Function Called'); 
    this.appComponent.variable1 = false; 
    } 
} 


<button (click)='Fun()'>Change Variable</button> 

{{appComponent.variable1}} 

工作實例http://plnkr.co/edit/fpYFueOnkm5sa4JfG7uX?p=preview

6

如果組件之間沒有關係(我的意思是父/子),你需要使用一個共享的服務與EventEmitter財產。一個組件將基於它發出一個事件,並通過訂閱EventEmitter來通知這個其他組件。當接收到的事件,該組件可以設置用來顯示/隱藏按鈕的屬性...

  • 共享服務

    @Injectable() 
    export class SharedService { 
        onMainEvent: EventEmitter = new EventEmitter(); 
    } 
    

    別忘了在自舉定義相應的供應商函數能夠爲整個應用程序共享相同的服務實例:`bootstrap(AppComponent,[SharedService]);

  • AppComponent組件

    @Component({ ... }) 
    export class AppComponent { 
        onMain: boolean = false; 
        constructor(service: MenuService) { 
        sharedService.onMainEvent.subscribe(
         (onMain) => { 
         this.onMain = onMain; 
         } 
        ); 
    } 
    

    }

  • MainComponent組件:

    export class MainComponent { 
        constructor(private service: SharedService) { 
        } 
    
        updateOnMain(onMain):void { 
        this.service.onMainEvent.emit(onMain); 
        } 
    } 
    

這些問題的更多詳細信息:

0

如果您有父母/子女關係,則可以使用事件發射器僅使用幾行代碼來翻轉變量。無需編寫完整的共享服務。

app.component.ts

@Component({ 
    selector: 'my-app', 
    template: ` 
    <nav *ngIf="onMain == false" (observableEvent)="onMain == true"> 
     Hello 
    </nav> 
    ` 
}) 

main.component.ts

import {AppComponent} from '../app.component'; 
import { Output, EventEmitter } from '@angular/core'; 

@Component({ 
    selector: 'main-app', 
    template: `` 
}) 

export class MainComponent{ 
    @Output() observableEvent: EventEmitter<any> = new EventEmitter<any>(); 

    constructor() { 
     this.appComponent = AppComponent; 
     this.observableEvent.emit(); 
    } 
} 
相關問題