使用Typescript在Angular 2中调用其他组件函数

阿里·萨贾德·里兹维(ALI Sajjad Rizvi)

伙计们,我是Angular 2的新手,基本上我想在另一个组件中使用一个组件的值,以便能够基于该值填充数据。

基本上,我有三个组件App.Component,Category.Component和Products.Component。

App.Component是两个组件的父级。

这是Category.Component代码

@Component({
selector: 'Categories',
template: `<li *ngFor="#category of categories">
                <a class="" href="{{category.Id}}" (click)="getCategoryProducts(category)">{{category.Name}}</a>
           </li>`,
providers :[CategoryService]

})
export class CategoriesComponent {
getData: string;
private categories: CategoryModel[] = [];
private products:ProductModel[] = [];
private productsComponent:ProductsComponent;
constructor(private _categoryService : CategoryService){
    this._categoryService.getCategories()
    .subscribe(
        a=>{
            this.categories = a;
        }
    );
    console.log(this.getData);

}

getCategoryProducts(category:CategoryModel)
{
    this._categoryService.getProducts(category.Id)
    .subscribe(
        a=>{
            this.products = a;
            this.productsComponent.populateProducts(this.products);
        }
    );
}


}

这是Products.Component代码

@Component({
selector: 'products',
template: `<div class="products-wrapper grid-4 products clearfix loading">
            <div *ngFor="#product of products"  (click)="getProduct(product)" class="product">
                <div class="product-inner" style="background:url({{product.pictureUrl}})">
                    <div class="time-left">
                        <span class="text">Hourly Deal</span>
                        <ul class="countdown clearfix">
                            <li> 
                                <div class="text">
                                    <span class="hours">00</span>
                                </div>
                            </li>

                            <li> 
                                <div class="text">
                                    <span class="minutes">00</span>
                                </div>
                            </li>
                            <li> 
                                <div class="text">
                                    <span class="seconds">00</span>
                                </div>
                            </li>
                        </ul>
                    </div>
                    <span class="discount-tag">{{product.discount}}%</span>

                </div>
            </div>
            </div>`,
            providers :[CategoryService]

})
@Injectable()
export class ProductsComponent {
private product:ProductModel;
private products: ProductModel[] = [];
constructor(private _categoryService : CategoryService)
{
    this._categoryService.getProducts(0)
    .subscribe(
        a=>{
            this.products = a;
        }
    );
}
getProduct(product:ProductModel)
{
    alert(product.productId);
    this.product = product;
}
populateProducts(products: ProductModel[] = [])
{
    this.products = products;
}
 }

基本上,我想将商品从类别组件的getCategoryProducts函数发送到商品组件,以便可以填充商品。

请帮帮我,谢谢

蒂埃里圣堂武士

在主要组件中,您可以将产品组件的一个实例提供给以下类别:

<categories [productsComponent]="productsComponent"></categories>
<products #productsComponent></products>

您需要为该productsComponent字段添加输入

@Component({
  (...)
})
export class CategoriesComponent {
  getData: string;
  private categories: CategoryModel[] = [];
  private products:ProductModel[] = [];
  @Input() // <------
  private productsComponent:ProductsComponent;
  (...)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Angular:从其他组件调用函数

来自分类Dev

如何从其他文件使用Angular 2组件中的javascript函数

来自分类Dev

Angular 2+ 从服务中的函数返回值到其他服务中的函数调用

来自分类Dev

[angular2] [ngModules]如何从其他模块调用组件?

来自分类Dev

Angular2:从其他组件调用方法或变量

来自分类Dev

Angular 2 组件函数调用

来自分类Dev

Angular2:在回调函数内部调用其他函数

来自分类Dev

访问Angular中其他组件的变量

来自分类Dev

调用在 TypeScript 中其他模块中声明的别名的函数

来自分类Dev

在不使用全局函数的情况下在其他函数中调用变量

来自分类Dev

在TypeScript中,当调用函数时,为什么可以提示null参数具有其他类型?

来自分类Dev

如何从TypeScript从Node.js中的其他文件调用JS函数

来自分类Dev

如何使用 angular 2 在组件类上的 ngAfterViewInit() 中调用 $( document ).ready() jQuery 函数

来自分类Dev

如何使用 Angular 2/4 在 Typescript 类中调用外部 javascript 库函数?

来自分类Dev

在 Angular 2 上显示来自其他组件的模态组件

来自分类Dev

函数调用未在 angular 的其他异步函数内发生

来自分类Dev

从angular2中的路由器组件调用父组件函数

来自分类Dev

调用嵌套组件函数angular2

来自分类Dev

在 angular 2 中从 html2canvas 调用 Typescript 函数

来自分类Dev

在Ruby中使用eval函数调用其他函数

来自分类Dev

TypeScript 从其他模块调用静态函数

来自分类Dev

codeigniter-使用模型导致控制器调用模型中的其他函数

来自分类Dev

在 R Shiny 中调用 tabPanel 时如何使用 lapply 或其他高阶函数

来自分类Dev

如何在Angular 2组件中测试RouteConfig和其他装饰器

来自分类Dev

调用组件函数时,self.context不是Angular2中的函数

来自分类Dev

使用Typescript获取Angular 2中的属性

来自分类Dev

使用Typescript获取Angular 2中的属性

来自分类Dev

Angular 2-在组件中使用Observables向其他组件发送值

来自分类Dev

在Angular2中将数据从组件传递到其他组件的技术之间有什么区别?

Related 相关文章

  1. 1

    Angular:从其他组件调用函数

  2. 2

    如何从其他文件使用Angular 2组件中的javascript函数

  3. 3

    Angular 2+ 从服务中的函数返回值到其他服务中的函数调用

  4. 4

    [angular2] [ngModules]如何从其他模块调用组件?

  5. 5

    Angular2:从其他组件调用方法或变量

  6. 6

    Angular 2 组件函数调用

  7. 7

    Angular2:在回调函数内部调用其他函数

  8. 8

    访问Angular中其他组件的变量

  9. 9

    调用在 TypeScript 中其他模块中声明的别名的函数

  10. 10

    在不使用全局函数的情况下在其他函数中调用变量

  11. 11

    在TypeScript中,当调用函数时,为什么可以提示null参数具有其他类型?

  12. 12

    如何从TypeScript从Node.js中的其他文件调用JS函数

  13. 13

    如何使用 angular 2 在组件类上的 ngAfterViewInit() 中调用 $( document ).ready() jQuery 函数

  14. 14

    如何使用 Angular 2/4 在 Typescript 类中调用外部 javascript 库函数?

  15. 15

    在 Angular 2 上显示来自其他组件的模态组件

  16. 16

    函数调用未在 angular 的其他异步函数内发生

  17. 17

    从angular2中的路由器组件调用父组件函数

  18. 18

    调用嵌套组件函数angular2

  19. 19

    在 angular 2 中从 html2canvas 调用 Typescript 函数

  20. 20

    在Ruby中使用eval函数调用其他函数

  21. 21

    TypeScript 从其他模块调用静态函数

  22. 22

    codeigniter-使用模型导致控制器调用模型中的其他函数

  23. 23

    在 R Shiny 中调用 tabPanel 时如何使用 lapply 或其他高阶函数

  24. 24

    如何在Angular 2组件中测试RouteConfig和其他装饰器

  25. 25

    调用组件函数时,self.context不是Angular2中的函数

  26. 26

    使用Typescript获取Angular 2中的属性

  27. 27

    使用Typescript获取Angular 2中的属性

  28. 28

    Angular 2-在组件中使用Observables向其他组件发送值

  29. 29

    在Angular2中将数据从组件传递到其他组件的技术之间有什么区别?

热门标签

归档