Original-ANGULARJS2 communication between different components

Source: Internet
Author: User
Tags export class

AngualrJs2 The official method is to @input, @Output to achieve the mutual value between components, and the components must be a parent-child relationship, the following gives you a simple way to achieve the transfer of values between components, not only the parent-child component, cross-module components can also achieve the value

/**. Define a service as the medium for passing parameters */@Injectable () Export class prepservice{  //define a property as a pass-through parameter between components, or it can be an object or method      Profileinfo:any;    }
/**. Pass the parameters of the component, I have a simple demonstration here, directly in the constructor implementation of the parameter */@Component ({  selector: ' XXXXXXX ',  templateurl: "./xxxxxx.html",  styleurls:["./xxxxxxx.css"]}) Export class Reportcomponent {  //define the parameters to pass (here is an object or method)          reponseprep:any ={    name: Bacon Bean curd,    Address: "Central Europe Pork"  }  //constructor Injection Prepservice service      constructor (private Ps:prepservice) {    // Assigns the current component parameter to Prepservice's ProfileInfo property    ps.profileinfo = This.reponseprep;  }}
  /**. The component that accepts the parameter */@Component ({  selector: ' yyyyyy ',  templateurl: "./yyyyyyyy.html",  styleurls:["./ Yyyyyyy.css "]}) Export class Commandcomponent {  //define parameter to receive value Requestprep:any from Prepservice service ProfileInfo property  ;   Constructor injection Prepservice Service      constructor (private Ps:prepservice) {    // Assigns the value of the ProfileInfo property of the Prepservice to the value of the Requestprep implementation component    This.requestprep = Ps.profileinfo;  }}

Idea: Defining a service as a medium for passing parameters is injected into the constructor of the component to which the parameter is being passed, and then assigning values and values to the service's Inside property (the reference vector) to implement the parameters between the components

Above demo just give a simple idea to everyone, everyone can play freely

Original-ANGULARJS2 communication between different components

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.