【问题标题】:Angular2 :Sending data from a component to a component via serviceAngular2:通过服务将数据从组件发送到组件
【发布时间】:2016-07-24 08:51:33
【问题描述】:

我正在从一个组件中捕获数据,并尝试通过服务将其发送到另一个组件

Component1(开始):单选框 查看

                <md-radio-button
                    *ngIf="item.id===1"
                    value="{{item.value}}"
                    class="{{item.class}}"
                    checked="{{item.checked}}"
                    (click)="onSelectType1(item)"> //here i'm catching the value
                {{item.label}}
            </md-radio-button>

ts 代码

public  onSelectType1(selected:any){
        this.formeService.type1Change(selected.nb)
}

服务:

@Injectable()
export class FormeService{
public type1S : any;

public  type1Change(selected):any

{

    this.type1S=selected;  //here i'm catching it into the service

 }

组件 2:(结束):简单视图

ts 代码

export class ContentComponent{

constructor(public BackService : BackgroundService ,
                public formeService: FormeService)
    {
        console.log(this.formeService.type1S) 



////// that diplays me in the console that it's undefined !!!!!!!!!!!!!!!!! 


The probleme is HERE : how may i access to the value of my variable in this part 



}

!!!!!!!!!同时在视图中它向我显示值:

{{formeService.type1S}}  // this works normally

谁能告诉我如何在“端组件 ts 代码”中显示我的数据值?????

【问题讨论】:

  • 如果您不认为这是重复的,请添加评论以重新打开它。
  • 问题是如何在 ts 代码中而不是在视图中重新使用我的数据的值,使用主题可以传递数据并在视图中显示它但是这里的上下文是如何显示它代码部分
  • .subscribe( 的部分是在 TypeScript 中获取服务中值更改的通知。
  • this.formeService.type1S.subscribe( type =>this.testVariable= type); console.log(this.testVariable) =======> 未定义
  • 同时:在视图中 {{testVariable}} 完美运行

标签: typescript angular angular2-routing angular2-directives angular2-services


【解决方案1】:

一个完整的例子Plunker

import {Injectable, Component, Directive} from 'angular2/core'
import { BehaviorSubject } from 'rxjs/subject/BehaviorSubject';
import 'rxjs/Rx';

@Injectable()
export class FormeService {
  public type1S$:BehaviorSubject<number> = new BehaviorSubject<number>(null);

  // when new values are set notify subscribers
  set type1S(value:number) {
    this.type1S$.next(value);  
  }
}

@Component({
  selector: 'content-comp',
  template: `
<div>{{value}}</div>
`})
export class ContentComponent {
  value:number;

  constructor(public formeService: FormeService) {
    // subscribe to updates
    this.formeService.type1S$.subscribe(val => {
      this.value = val;
    });
  }
} 

@Component({
  selector: 'other-comp',
  template: `
<button (click)="clickHandler()">count</button>
`})
export class OtherComponent {
  counter:number = 0;
  constructor(private formeService: FormeService) {}

  // set new values      
  clickHandler() {
    this.formeService.type1S = this.counter++;
  }
} 

@Component({
  selector: 'my-app',
  providers: [FormeService],
  directives: [ContentComponent, OtherComponent]
  template: `
  <h2>Hello {{name}}</h2>
  <content-comp></content-comp>
  <other-comp></other-comp>
`
})
export class App {

}

【讨论】:

  • 非常感谢您的回答和宝贵的帮助,但我不知道问题是否仍然存在,如果我想在内容组件中显示“value”的值,请在这个 plunker 中显示,即使点击 2 或 3 次,它也会显示 null(第一个值)
  • console.log(this.value); // null ALWAYS (INSIDE CONTENT-COMPONENT)
  • 您是否检查了 Plunker(我的答案顶部的链接)?内容组件显示更新后的值。
  • 是的,我做到了,只需将 console.log 放在 content-comp 的底部并检查导航器控制台,即使它在模板视图中完美运行,它也会显示“null”
  • 你能更新我的 plunker 以显示你做了什么吗?
猜你喜欢
  • 1970-01-01
  • 2017-10-19
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 2016-03-18
  • 2021-04-11
相关资源
最近更新 更多