gpt4 book ai didi

Angular2 指令 - 字符串问题

转载 作者:搜寻专家 更新时间:2023-10-30 21:24:23 25 4
gpt4 key购买 nike

我们在Angular2 中创建了一个指令,并且有一个input 属性应该接收一个string。问题是,我们无法传递字符串。

如果我们尝试这样做:

<learning-requirements [title]="You should be able to!" [content]="requirements"></learning-requirements>

它不起作用,所以我们必须这样做,然后它才能起作用:

<learning-requirements [title]="'You should be able to!'" [content]="requirements"></learning-requirements>

这是我们的指令:

@Component({
selector: "learning-requirements",
directives: [IONIC_DIRECTIVES],
templateUrl: "build/pages/learning/components/requirements/requirements.html"
})
export class RequirementsComponent {
@Input() public title: string;
@Input("content") public items: Array<Requirement>;
}

知道如何在不显式使用单引号的情况下处理字符串吗?

最佳答案

基本上,只要你用 [] 方括号包装属性,它就会用组件的上下文(this)评估属性值(表达式)。

由于您直接传递一个值,您可以直接指定 title 属性而不用 [] 方括号,这意味着该值您传递的内部属性不会根据组件的上下文(this)进行评估。

标记

<learning-requirements 
title="You should be able to!"
[content]="requirements">
</learning-requirements>

关于Angular2 指令 - 字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39469688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com