如何重新加载当前的 Angular 2 组件 [英] How to reload the current Angular 2 Component

查看:59
本文介绍了如何重新加载当前的 Angular 2 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Angular 2 中再次重新加载同一个组件?

这是我的代码如下:

import { Component, OnInit, ElementRef, Renderer } from '@angular/core';从'@angular/router' 导入 { Router, ActivatedRoute, Params };从'../_models/index'导入{productModel};从'../_services/index'导入{ categoryListService };@成分({选择器:应用产品",templateUrl: 'product.component.html',styleUrls: ['product.component.css']})导出类 productComponent 实现 OnInit {uidproduct:产品型号;参数:数字;构造函数(私有元素引用:元素引用,私人路线:ActivatedRoute,专用路由器:路由器,私有类别列表服务:类别列表服务){}ngOnInit() {this.route.params.subscribe(product => {console.log('记录子产品obj', product);});this.uidproduct = JSON.parse(sessionStorage.getItem('product'));var s = document.createElement("脚本");s.type = "文本/javascript";s.src = "http://this/external/script/needs/to/be/loaded/each/time.js";this.elementRef.nativeElement.appendChild(s);}下一个产品(){让我 = this.uidproduct.order;this.categoryListService.findNextproduct(this.uidproduct);this.param = ++i;this.router.navigate([`/product/${this.param}`]);}}

nextproduct() 绑定到模板中的点击事件.

uidproduct 是一个具有许多属性的 JSON 对象,我正在使用 {{uidproduct.classname}}

更新 DOM

我在模板中使用这个:

当我单击 <button (click)="nextproduct()"> 时,它会更改 DOM 中的类属性,但我需要重新加载组件以使外部脚本具有效果.

解决方案

您可以使用 *ngIf 重新渲染模板的内容:

@Component({选择器:'...',模板:`<ng-container *ngIf="!rerender">模板内容在这里</ng-容器>`})导出类 MyComponent {重新渲染 = 假;构造函数(私有 cdRef:ChangeDetectorRef){}doRerender() {this.rerender = true;this.cdRef.detectChanges();this.rerender = false;}}

How can I reload the same component again in Angular 2?

Here is my code below:

import { Component, OnInit, ElementRef, Renderer } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { productModel } from '../_models/index';
import { categoryListService } from '../_services/index';

@Component({
  selector: 'app-product',
  templateUrl: 'product.component.html',
  styleUrls: ['product.component.css']
})
export class productComponent implements OnInit {
  uidproduct: productModel;
  param: number;
  constructor(
    private elementRef: ElementRef,
    private route: ActivatedRoute,
    private router: Router,
    private categoryListService: categoryListService) { }

  ngOnInit() {
    this.route.params.subscribe(product => {
      console.log('logging sub product obj', product);
    });
    this.uidproduct = JSON.parse(sessionStorage.getItem('product'));
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = "http://this/external/script/needs/to/be/loaded/each/time.js";
    this.elementRef.nativeElement.appendChild(s);
  }
  nextproduct(){ 
    let i = this.uidproduct.order;
    this.categoryListService.findNextproduct(this.uidproduct);
    this.param = ++i;
    this.router.navigate([`/product/${this.param}`]);
  }
}

nextproduct() is bound to a click event in the template.

The uidproduct is a JSON object that has a number of properties and i'm updating the DOM with {{uidproduct.classname}}

I'm using this in the template like this:

<div id="selected-product" class="{{uidproduct.classname}}">

When I click the <button (click)="nextproduct()"> it will change the class property in the DOM but I need to reload the component for the external script to have effect.

解决方案

You can use *ngIf to re-render the content of a template:

@Component({
  selector: '...',
  template: `
<ng-container *ngIf="!rerender">
 template content here
</ng-container>`
})
export class MyComponent {
  rerender = false;
  constructor(private cdRef:ChangeDetectorRef){}
  doRerender() {
    this.rerender = true;
    this.cdRef.detectChanges();
    this.rerender = false;
  }
}

这篇关于如何重新加载当前的 Angular 2 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆