我是 Angular 的新手,好几天都无法解决这个问题。
我创建了一个组件,我想在其中传递我需要的参数,但是问题是第一个组件正确显示了值,而其余元素显示了第一个的值。
起初我试图传递这样的参数:
<app-pr1-button class="grid-item grid-item-1"
[name]="'name1'"
[label]="'label1'"
[text]="'text1'"
[inputtext]="['m1','m2']"
>
</app-pr1-button>
<app-pr1-button class="grid-item grid-item-2"
[name]="'name2'"
[label]="'label2'"
[text]="'text2'"
[inputtext]="['n1','n2']"
>
</app-pr1-button>
元素名称显示在父组件中,scss 帮我解决了这个问题,[name] 在两个组件中都正确显示,但是弹出的其他参数(我也使用 scss 完成了此操作)与第一个组件完全相同,即是,label1,text1 和'm1','m2'。
后来我开始像这样传递参数:
<ng-template #temp>
<h1>label1</h1>
<p>text1</p>
<app-input-data [text]="['m1','m2']"></app-input-data>
</ng-template>
<app-pr1-button class="grid-item grid-item-1" [name]="'name1'" [template]="temp"></app-pr1-button>
<ng-template #temp1>
<h1>label2</h1>
<p>text2
</p>
<app-input-data [text]="['m1','m2']"></app-input-data>
</ng-template>
<app-pr1-button class="grid-item grid-item-2" [name]="'name2'" [template]="temp1"></app-pr1-button>
孩子ts:
export class Pr1ButtonComponent implements AfterViewInit{
@Input() template : TemplateRef<any>;
// @Input() label: string | undefined;
// @Input() text: string | undefined;
@Input() name: string | undefined;
// @Input() inputtext: string[] | undefined;
@Input() longread: string | undefined;
@ViewChild('viewcontainer',{read:ViewContainerRef}) viewcontainer : ViewContainerRef;
ngAfterViewInit() {
this.viewcontainer.createEmbeddedView(this.template);
}
}
子html:
<a class="button" href="#popup">{{name}}</a>
<p class="longread">{{longread}}</p>
<div class="popup" id="popup">
<div class="popup-inner">
<div class="popup__photo">
<img src=""
alt="">
</div>
<div class="popup__text">
<div #viewcontainer></div>
<!-- <h1>{{label}}</h1>-->
<!-- <p>{{text}}</p>-->
</div>
<a class="popup__close" href="#">X</a>
</div>
</div>
如果我从 scss 中删除我的样式,那么这些是我传递的参数,所以我倾向于假设关键是 scss 不允许更新数据。有人可以建议如何解决这个问题吗?
附言
文案:
@import url('https://fonts.googleapis.com/css?family=Raleway:300,400,600&subset=latin-ext');
$main-color: #9191E9;
* {
box-sizing: border-box;
}
html, body {
font-family: 'Raleway', sans-serif;
font-size: 16px;
}
@media screen and (max-width: 768px) {
html, body {
font-size: 12px;
}
}
.longread{
padding-top: 10px;
}
.container {
background-color: $main-color;
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
}
.button {
color: #202230;
text-decoration:none;
}
.popup {
display: flex;
align-items: center;
justify-content: center;
position: fixed;
width: 100vw;
height: 100vh;
bottom: 0;
right: 0;
background-color: rgba(0, 0, 0, .80);
z-index: 2;
visibility: hidden;
opacity: 0;
overflow: hidden;
transition: .64s ease-in-out;
&-inner {
position: relative;
bottom: -100vw;
right: -100vh;
display: flex;
align-items: center;
max-width: 800px;
max-height: 600px;
width: 60%;
height: 80%;
background-color: #fff;
transform: rotate(32deg);
transition: .64s ease-in-out;
}
&__photo {
display: flex;
justify-content: flex-end;
align-items: flex-end;
width: 40%;
height: 100%;
overflow: hidden;
img {
width: auto;
height: 100%;
}
}
&__text {
display: flex;
flex-direction: column;
justify-content: center;
width: 60%;
height: 100%;
padding: 4rem;
h1 {
font-size: 2rem;
font-weight: 600;
margin-bottom: 2rem;
text-transform: uppercase;
color: #0A0A0A;
}
p {
font-size: .875rem;
color: #686868;
line-height: 1.5;
}
}
&:target {
visibility: visible;
opacity: 1;
.popup-inner {
bottom: 0;
right: 0;
transform: rotate(0);
}
}
&__close {
position: absolute;
right: -1rem;
top: -1rem;
width: 3rem;
height: 3rem;
font-size: .875rem;
font-weight: 300;
border-radius: 100%;
background-color: #0A0A0A;
z-index: 4;
color: #fff;
line-height: 3rem;
text-align: center;
cursor: pointer;
text-decoration: none;
}
}