【发布时间】:2018-09-15 22:12:26
【问题描述】:
在 Angular4 中,视图 (.html) 上的 属性绑定 从逻辑文件 (.ts) 中获取值
这在代码中效果很好:
<img [src]="sourceValue">
这在代码中也很有效:
<button [disabled]="isDisabled">
为什么这不起作用?
<p [style]="paragraphStyle"> This is a paragraph.</p>
abc.component.ts
isDisabled:boolean = true;
sourceValue:string = "./assets/hermione.jpg";
paragraphStyle:string = "background:red; color:yellow";
我知道ngStyles和ngClass的用法,我只是想问一下为什么在上述情况下属性绑定不起作用。最后 --- 只是一个简单的“内联 CSS 样式”,如果值取自 .ts 文件并添加到段落中 'style' 属性前面的 html sn-p。
【问题讨论】:
-
你在这里做了你不应该做的事情,组件中的 TypeScript 文件用于在 html 视图和服务之间编组数据,而 html 文件用于演示。如果您的 TypeScript 中有图像名称和样式,那么您正在错误地考虑关注点分离。
标签: javascript angular property-binding