简体   繁体   English

在 Angular 上的另一个组件(between.ts 文件)中使用一个变量

[英]Use a variable from one component in another (between .ts files) on Angular

So basically I have a todo list being added dynamically and I want to exclude the items, however the list itself is outside the task component:所以基本上我有一个动态添加的待办事项列表,我想排除这些项目,但是列表本身在任务组件之外:

In the task.component.html, the list is being iterated to be displayed:在task.component.html中,正在迭代显示列表:

<ul class="list-group" *ngFor="let taskElement of taskElements; let i=index" [taskElement]="taskElement">
<div class="d-flex justify-content-center">
    <li class="list-group-item">
        <span>
            {{element.description}}
        </span>
        <button class="btn btn-danger" style="float: right;" type="button">Remove
            Task</button>
    </li>

However the list is defined on the file app.component.ts:但是,该列表是在文件 app.component.ts 中定义的:

public containsTask = false;
  taskElements: { description: string, isDone: boolean }[] = [];

  onTaskCreated(taskData: { taskDescription: string }) {
    this.taskElements.push({
      description: taskData.taskDescription,
      isDone: false
    });
    this.containsTask = true;
  }

How can I use the array taskElements (declared in app.component.ts) in the task.component.ts?如何在 task.component.ts 中使用数组 taskElements(在 app.component.ts 中声明)?

Thanks!谢谢!

You can use a service to centralise the data.您可以使用服务来集中数据。 The service can then be injected into both components and this way you can access the array defined inside of the service.然后可以将该服务注入到两个组件中,这样您就可以访问服务内部定义的数组。

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

相关问题 从一个组件到另一个组件的变量在 angular 9 中不起作用 - variable from one component to another is not working in angular 9 Angular 2 将变量从一个组件传递到另一个组件 - Angular 2 pass variable from one component to another 在 component.ts Angular 中使用可变颜色 - Use variable color in component.ts Angular 如何在 Angular 中的另一个组件中使用变量 - How to use a variable from a component in another in Angular 如何在Angular 2中使用一个类(组件)的变量在另一个类(组件)中? - How to use variable of one class(component) in another class(component) in Angular 2? 如何将一个组件中的变量用于 angular 中的另一个组件 - how to use the variable in one component to another component in angular 如何以角度从一个ts文件获取变量到所有其他ts文件 - how to get variable from one ts file into all other ts files in angular 从一个组件中的服务访问变量到Angular 7中的另一个组件 - accessing variable from service in one component to another component in Angular 7 如何以角方式将变量从一个组件访问另一个组件? - How to access variable from one component into another component in angular? Angular 2,无法从另一个Component访问一个Component中的变量 - Angular 2, Cant able to access a variable in one Component from another Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM