2016-03-02 105 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(); 
    } 
} 
相关问题