简体   繁体   English

如何在Ionic 2中跨组件共享变量?

[英]How to share variables across components in Ionic 2?

In Ionic 1 (With Angular 1), I can create a $scope.abc on the ngApp layer and other ngControllers can inherit $scope.abc with ngModel, so that when a text area in one controller changes, the others will change accordingly via ngModel. 在Ionic 1(使用Angular 1)中,我可以在ngApp层上创建$scope.abc ,其他ngController可以使用ngModel继承$scope.abc scope.abc,这样当一个控制器中的文本区域更改时,其他控制器将通过以下方式相应地更改ngModel。

How may I achieve "synchronized text area" with Ionic 2 (and Angular 2)? 如何使用Ionic 2(和Angular 2)实现“同步文本区域”?

Here are some of my attempts: 这是我的一些尝试:

  1. Injecting MyApp in the constructor of the components: the [(ngModel)]="myApp.abc" will result in a undefined error in console of failing to resolve context.myApp.abc... 将MyApp注入组件的构造函数中: [(ngModel)]="myApp.abc"将导致无法解析context.myApp.abc的控制台出现未定义的错误。
  2. Creating a Service with a setter. 用设置器创建服务。 Use [(ngChange)] to call the setter and use getter in the constructor of another component: the text area doesn't change after the component is instantiated. 使用[(ngChange)]调用setter并在另一个组件的构造函数中使用getter:实例化该组件后,文本区域不会更改。 Using ViewOnInit instead of Constructor doesn't help either. 使用ViewOnInit代替Constructor也不起作用。 Is there an event handler for "component is shown on screen"? 是否有用于“组件显示在屏幕上”的事件处理程序?

Create a service and add this service to the shared parent component (or if there isn't one, add it to app component's) list of providers. 创建一项服务,然后将此服务添加到共享的父组件(如果没有,则将其添加到应用程序组件的)提供者列表。 Make sure to import the service in each component that needs to use the service. 确保在需要使用服务的每个组件中导入服务。 Then inject the service in constructor of each component that needs it. 然后将服务注入需要它的每个组件的构造函数中。

Plunkr example: https://plnkr.co/l3BlNdjQfzPIIGPSXp9n?p=preview Plunkr示例: https ://plnkr.co/l3BlNdjQfzPIIGPSXp9n p = preview

// >>>home.ts
import { Component } from "@angular/core";
import { Component1 } from "./component1.ts";
import { Component2 } from "./component2.ts";

import { YearService } from "./year.service.ts"
@Component({
  templateUrl:"home.html",
  directives: [Component1, Component2],
  providers: [YearService]
})
export class HomePage {

  constructor() {   }

}

// >>>home.html
<ion-header>
  <ion-navbar primary>
    <ion-title>
      Ionic 2
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <comp-one></comp-one>
  <comp-two></comp-two>
</ion-content>

// >>>component1.ts
import { Component } from "@angular/core";
import { YearService } from "./year.service.ts";

@Component({
  templateUrl: "./component1.html",
  selector: "comp-one"
})
export class Component1 {
  constructor(yearSvc: YearService) {   
    this.year = yearSvc.getYear();
  }
}

// >>>compnent1.html
<p> Year from component 1 using a shared service: <strong>{{year}}</strong></p>

// >>>component2.ts
import { Component } from "@angular/core";
import { YearService } from "./year.service.ts";

@Component({
  templateUrl: "./component2.html",
  selector: "comp-two",
})
export class Component2 {
  constructor(yrSvc: YearService) {
    this.year = yrSvc.getYear();
  }
}

// >>> component2.html
<p> Year from component 2 using a shared service: <strong>{{year}}</strong></p>

// >>> year.service.ts
import { Injectable } from '@angular/core';

@Injectable()
export class YearService {
  getYear() {
    return new Date().getFullYear();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM