Communication instance code between different components of Angularjs2 and angularjs2

Source: Internet
Author: User

Communication instance code between different components of Angularjs2 and angularjs2

The official method of AngualrJs2 is to use @ Input and @ Output to transfer values between components and ensure the parent-child relationship between components. The following is a simple method, implement data transfer between components, not only the parent and child components, but also the cross-module components.

/*** 1. define a service as the medium for passing parameters * // @ Injectable () export class PrepService {// define an attribute as a passing parameter between components, it can also be an object or method profileInfo: any;}/*** 2. the Component for passing parameters. In this simple demonstration, the parameter */@ Component ({selector: 'xxxxxxxx', templateUrl :". /XXXXXX.html ", styleUrls :[". /XXXXXXX.css "]}) export class ReportComponent {// defines the parameter to be passed (this is an object or a method) reponsePrep: any = {name:" bacon ", address: ""} // constructor (private ps: PrepService) injected into the PrepService {// assign the current component parameter to the profileInfo attribute ps of the PrepService. profileInfo = this. reponsePrep;}/*** 3. component that accepts parameters */@ Component ({selector: 'yyyy', templateUrl :". /YYYYYYYY.html ", styleUrls :[". /YYYYYYY.css "]}) export class commandComponent {// defines the parameter to receive the value requestPrep: any from the PrepService profileInfo attribute; // The constructor (private ps: prepService) {// assign the value of profileInfo attribute of PrepService to requestPrep to implement this. requestPrep = ps. profileInfo ;}}

Idea: define a service as a medium for passing parameters and inject it into the constructor of the component for passing parameters, and then attribute the Service (passing parameter Media) to assign values and values for passing parameters between components.

The above is the communication instance code between different Angularjs2 components introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in time!

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.