【发布时间】:2018-12-16 17:02:03
【问题描述】:
所以假设我有一个像这样的 Angular 4 组件:
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
})
export class HomeComponent implements OnInit {
printThisValue = 'customAttr';
constructor(){}
}
如何打印 HTML 标记内的值,如下所示:
<div class="myStyle" {{ printThisValue }} ></div>
上面的 HTML 代码似乎不起作用。我知道您可以像这样打印值:
<div class="myStyle">{{ printThisValue }}</div>
// or like this
<div class="myStyle {{ printThisValue }}"></div>
但是我该怎么做呢?
【问题讨论】:
-
假设你想绑定一个属性到一个变量;如果你知道你可以做的属性名称:
<div class="myStyle" [attr.customAttr]="printThisValue"></div> -
谢谢@dotoconnor,这可以工作,但是具有特殊字符(如“[handle]”)的值呢,因为我想在标签中添加的值有一个特殊字符,我相信做 [attr.[handle]] 或 [attr."[handle]"] 不起作用。 :(
-
concretepage.com/angular-2/…查看这个链接,希望你能得到你需要的东西
标签: javascript html angular