我想更改页面内容而不重新加载角度6

时间:2019-02-21 10:19:32

标签: javascript angular typescript components

我是新手。我想动态更改页面内容,或者可能要显示其中包含新内容的新组件。我的网站上有卡。请参考链接 Cards

我想更改页面的内容,并在卡上单击的每个按钮上显示不同的内容。我已经创建了一个方法及其正确的方法。

Component.html

<a href="#" class="btn btn-primary" (click)="onFirstClick()">Go somewhere</a>

component.ts

onFirstClick() {
}

我需要创建一个新组件来显示新内容吗?我该怎么办?请帮助

1 个答案:

答案 0 :(得分:1)

好的取决于您要动态更改的内容,但是无论如何,Angular确实很擅长。例如,如果要在第一次单击时切换按钮的文本,则可以执行以下操作:

component.ts:

buttonTxt: string = 'Click me';

onFirstClick() {
   this.buttonTxt = 'Button Clicked';
}

component.html:

<a href="#" class="btn btn-primary" (click)="onFirstClick()">{{buttonTxt}}</a>

动态更改页面上的内容是angular的事情很好,并且有很多不同的方式:ngIf *(根据ts上的布尔变量隐藏或显示html组件)等等。

您可以在此处详细了解以下内容:https://medium.com/@DenysVuika/dynamic-content-in-angular-2-3c85023d9c36

相关问题