如何在angular2中单击按钮更改视图

Uidev123

我刚刚开始学习 angular 2。我想要实现的是当我点击项目视图时会改变并显示项目详细信息。

在此处输入图片说明

当我点击 60074190 时,视图应该会发生变化并显示这样的项目详细信息

在此处输入图片说明

我不知道如何实现这一点我有两个组件 1.item 和 2. 项目详细信息都在传输文件夹中。我正在使用传输路由来路由到项目页面,但现在单击项目名称我想更改我的视图并显示项目详细信息。请指导我如何做到这一点

莫鲁豺

1) 创建 STO 的对象 sto.model.ts

    export class Sto {
      id: number;
      trackingNumber: string;
      transferType: string; //or create an enum
    }

2) 创建一个 STO 的模拟来存储一些 Sto,稍后将由服务 sto.mock.ts 调用

    import { Sto} from "./sto.model.ts";
    export const STOS: Sto[] = {
    {id: 600071498}, trackingNumber: 'AB123', transferType: 'S2S'},
    {id: 600071577}, trackingNumber: 'CD123', transferType: 'S2S'}
    }

3)创建一个服务来调用组件获取所有产品,以及产品 Id , Sto.service.ts

    import { Injectable } from "@angular/core";
    import { Sto } from "./sto.model.ts";
    import { STOS } from "./sto.mock.ts";

    @Injectable()
    export class StoService {
       getStos(): Product[] {
         return STOS;
       }

       getSto(stoId: number): Product {
        let result: Product;
        result = STOS.find(x => x.id === stoId);

        if (result) {
           return result;
        } else {
          return new Sto();
        }
      }
    }

4) 在你的 ItemComponent import 'Router' from @angular/router,import sto 模型和服务注入 sto 服务,创建一个 Sto 属性,在 ngOnInit 中填充数据,并在你的 html 中绑定它

    import { Router } from "@angular/router";
    import { Component, OnInit } from '@angular/core';
    import { StoService } from "./stoService.service.ts";
    import { Sto } from "./sto.model.ts";

    @Component({
    selector: 'app-item',
    templateUrl: './item.component.html'

    export class NavigationComponent implements OnInit {
      stos: Sto[];
      sto: Product;

   constructor(private stoService: StoService, private route: 
   Router) {
      this.stos= []; //initialize the stos collection

      this.sto= new Product(); //initialze the sto
   }

   ngOnInit() {
        this.stos = this.stoService.getStos();

   }

   navigateToItemDetails(stoId) {
    this.route.navigate(['item-details', stoId]);
  }

})

在您的 sto.component.html 中,您必须将 STO # 设置为带有可点击事件的链接,可能是这样的

    <a href="javascript:;" (click)="navigateToItemDetails(sto.id)"> {{sto.Id}}></a>

5)在您的 ItemDetailsComponent 中,导入以下所有依赖项

    import { Component, OnInit, OnDestroy, OnChanges} from '@angular/core';
    import { Product } from "../models/product.model";
    import { Subscription } from 'rxjs/Rx';
    import { ActivatedRoute, Router } from '@angular/router';
    import { ProductService } from "../services/product.service";

    @Component({
      selector: 'app-item-details',
       templateUrl: './item-details.component.html'})

   export class ItemDetailsComponent implements OnInit, OnDestroy, OnChanges 
   {
     sto: Sto;
     stoId: string;
     private subscription: Subscription;

     constructor(private activatedRoute: ActivatedRoute, private route: 
      Router, private stoService: StoService) { }

     ngOnInit() { //subscribe to read the params sent from ItemComponent
      this.subscription = this.activatedRoute.params.subscribe((params) => {
        this.productId = params['id'];
        this.sto = this.stoService.getSto(this.stoId);
    });

    ngOnDestroy() {
    this.subscription.unsubscribe();
}

6) 如您所见,从 id 中,您通过服务遍历 sto mock 中的数据,并获取 sto 对象,稍后您可以在您的 html 中进行相应操作

 <p> Tracking #: {{sto.trackingNumber}}</p>

7)不要忘记在 app.module.ts 中注册您的服务和组件

   //other imports
   import { StoService } from "./sto.service";
   import { ItemComponent } from './item.component';
   import { ItemDetailsComponent } from './item-details.component';
   import { RouterModule } from '@angular/router';


   @NgModule({
     imports: [//othhermoduleimports, RouterModule],
     declarations: [//othercomponent, ItemComponent, ItemDetailsComponent]
     providers: [ StoService],

记得为此添加路由,你可以谷歌那个。它很容易

8)并添加到您的路线

    { path: 'item-details/:id', component: ItemDetailsComponent}
    // the :id will be the parameter sent to the ItemDetailsComponent

快乐编码,欢迎使用 angular :)

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在angular2中更改按钮文本并在单击事件上禁用按钮

当单击列表视图项中的按钮时,只有相应的按钮文本应在 angular2 nativescript 中更改

根据 Angular2 中的单击更改视图

如何在Angular2 +中单击重置按钮上的单选按钮重置?

如何在angular2中单击按钮显示/隐藏组件?

在 Angular 中,如何在单击时更改按钮的文本

如何在angular2中单击不同组件的后退按钮时重定向到页面?

单击Angular2时更改按钮外观

如何在三个JS中单击按钮更改多个视图

如何在单击按钮时显示隐藏的div并在Angular 2中隐藏单击的按钮?

如何在Angular2中监视对localStorage的更改?

如何在angular2中更改模型的值?

如何在Angular2 +中轻轻更改img src

我如何在使用 angular2 单击添加按钮时获得表单模型

如何创建新部分,单击angular2中的新按钮

单击按钮以在angular2中显示相同的数据

如何在SwiftUI中单击按钮打开Web视图?

如何在Angular中更改视图值?

如何在单击Angular2模板中的按钮时显示加载图像/微调框,直到收到服务器的响应

如何在ionic 2 angular 2中的单击事件中获取按钮的id和名称值

Google事件监听器触发后,Angular2中的更改后如何更新视图?

如何在Android中单击按钮更改笔的颜色

如何在JavaScript中单击按钮更改CSS类?

如何在Razor中单击按钮时更改模型值?

如何在android中单击按钮时更改ImageView?

如何在Android应用中多次单击按钮并更改模式

Angular2如何更改禁用按钮的颜色

如何根据 Angular 中的按钮类更改单击功能

如何在单击angular2本机脚本时打开按钮上的playstore应用程序URL