【问题标题】:How to send a value from one component to another?如何将值从一个组件发送到另一个组件?
【发布时间】:2016-04-20 14:29:49
【问题描述】:

我制作了一个组件,其中有一个输入字段和一个按钮。单击按钮时,我正在显示第二个组件。我想将数据从一个组件发送到另一个组件?

我如何将数据从一个组件发送到另一个..我需要发送输入值(无论用户在输入字段中输入什么)我需要在下一个组件或下一页上显示。点击按钮。如何发送数据? 这是我的笨蛋 http://plnkr.co/edit/IINX8Zq8J2LUTIyf4DYD?p=preview

import {Component,View} from 'angular2/core';
import {Router} from 'angular2/router';

@Component({
    templateUrl: 'home/home.html'
})


export class AppComponent {
   toDoModel;
  constructor(private _router:Router) {


  }

  onclck(inputValue){
    alert(inputValue)
    this._router.navigate(['Second']);
  }

}

【问题讨论】:

    标签: angular angular2-routing angular2-template angular2-directives


    【解决方案1】:

    您所要做的就是创建一个服务,将其注入两个组件中,将输入值分配给服务可验证并在另一个组件中访问它。 对不起兄弟,但不知道创建 plunker。这是代码:

    编辑:

    首先做这个数据服务:

    export class DataService{
         dataFromService;}
    

    然后将其注入您的第一个组件中:

    import {Component,View} from 'angular2/core';
    import {Router} from 'angular2/router';
    import {DataService} from 'path/to/dataservice';
    
    @Component({
    templateUrl: 'home/home.html'
    })
    
    
    export class AppComponent {
     toDoModel;
     constructor(private _router:Router, public dataService : DataService) {
    
    
    }
    
     onclck(inputValue){
      alert(inputValue)
      this.dataService.dataFromService = inputValue;
      this._router.navigate(['Second']);
     }
    
    }
    

    然后注入另一个组件并访问该值:

    import {Component,View} from 'angular2/core';
    import {DataService} from 'path/to/dataservice';
    export secondComponent{
       constructor(public dataService : DataService){
       console.log(this.dataService.dataFromService);
    }
    

    【讨论】:

      【解决方案2】:

      哦!!可能我回答这个问题来得太晚了! 但没关系。这可能会帮助您或其他人在使用路由器、共享服务和与共享服务一起使用的共享对象之间共享数据。我希望这肯定会有所帮助。

      Answer

      Boot.ts

      import {Component,bind} from 'angular2/core';
      
      import {bootstrap} from 'angular2/platform/browser';
      
      import {Router,ROUTER_PROVIDERS,RouteConfig, ROUTER_DIRECTIVES,APP_BASE_HREF,LocationStrategy,RouteParams,ROUTER_BINDINGS} from 'angular2/router';
      
      import {SharedService} from 'src/sharedService';
      
          import {ComponentFirst} from 'src/cone';
      import {ComponentTwo} from 'src/ctwo';
      
      
      @Component({
        selector: 'my-app',
        directives: [ROUTER_DIRECTIVES],
        template: `
          <h1>
            Home
          </h1> 
      
          <router-outlet></router-outlet>
            `,
      
      })
      
      @RouteConfig([
        {path:'/component-first', name: 'ComponentFirst', component: ComponentFirst}
        {path:'/component-two', name: 'ComponentTwo', component: ComponentTwo}
      
      ])
      
      export class AppComponent implements OnInit {
      
        constructor(router:Router)
        {
          this.router=router;
        }
      
          ngOnInit() {
          console.log('ngOnInit'); 
          this.router.navigate(['/ComponentFirst']);
        }
      
      
      
      }
      
          bootstrap(AppComponent, [SharedService,
          ROUTER_PROVIDERS,bind(APP_BASE_HREF).toValue(location.pathname)
          ]);
      

      第一组件

      import {Component,View,bind} from 'angular2/core';
      import {SharedService} from 'src/sharedService';
      import {Router,ROUTER_PROVIDERS,RouteConfig, ROUTER_DIRECTIVES,APP_BASE_HREF,LocationStrategy,RouteParams,ROUTER_BINDINGS} from 'angular2/router';
      @Component({
        //selector: 'f',
        template: `
          <div><input #myVal type="text" >
          <button (click)="send(myVal.value)">Send</button>
            `,
      
      })
      
      export class ComponentFirst   {
      
        constructor(service:SharedService,router:Router){
          this.service=service;
          this.router=router;
        }
      
        send(str){
          console.log(str);
          this.service.saveData(str); 
          console.log('str');
          this.router.navigate(['/ComponentTwo']);
        }
      
      }
      

      第二组件

      import {Component,View,bind} from 'angular2/core';
      import {SharedService} from 'src/sharedService';
      import {Router,ROUTER_PROVIDERS,RouteConfig, ROUTER_DIRECTIVES,APP_BASE_HREF,LocationStrategy,RouteParams,ROUTER_BINDINGS} from 'angular2/router';
      @Component({
        //selector: 'f',
        template: `
          <h1>{{myName}}</h1>
          <button (click)="back()">Back<button>
            `,
      
      })
      
      export class ComponentTwo   {
      
        constructor(router:Router,service:SharedService)
        {
          this.router=router;
          this.service=service;
          console.log('cone called');
          this.myName=service.getData();
        }
        back()
        {
           console.log('Back called');
          this.router.navigate(['/ComponentFirst']);
        }
      
      }
      

      SharedService 和共享对象

      import {Component, Injectable,Input,Output,EventEmitter} from 'angular2/core'
      
      // Name Service
      export interface myData {
         name:string;
      }
      
      
      
      @Injectable()
      export class SharedService {
        sharingData: myData={name:"nyks"};
        saveData(str){
          console.log('save data function called' + str + this.sharingData.name);
          this.sharingData.name=str; 
        }
        getData:string()
        {
          console.log('get data function called');
          return this.sharingData.name;
        }
      } 
      

      【讨论】:

      • 这正是我想要的。感谢上帝,我找到了这个解决方案。感谢@micronyks 的回答。 :) :)
      • @ananya 太好了,它帮助了你。
      【解决方案3】:

      最好有一个后端和一个服务调用后端来获取数据。 首先,您需要将输入数据发布到服务并通过 nodejs 或数据库从某个数据库中获取该数据。 这将是基于数据的一些主键获得它的最好方法 至少企业应用在现实中是如何工作的

      【讨论】:

        猜你喜欢
        • 2020-07-30
        • 1970-01-01
        • 2023-01-16
        • 2021-08-07
        • 1970-01-01
        • 2021-05-02
        • 2020-12-06
        • 1970-01-01
        • 2020-12-30
        相关资源
        最近更新 更多