在Angular中使用什么更好 - src或[src] [英] What's better to use in Angular - src or [src]

查看:84
本文介绍了在Angular中使用什么更好 - src或[src]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道什么建议用于Angular,标准html src 或Angular [src] ?为什么?

Just want to ask what is recommended to use with Angular, standard html src or Angular [src]? And why?

编辑:我的html组件中有以下代码:

Edit: I have following code in my html component:

<img class="logo" src="../app/media/appLogo.png">

没关系?如果没有,我应该如何改变它与 [src] 一起工作?

It is fine? If not, how should I change it to work together with [src]?

Edit2 :还有其他更好的方法,而不是普通的html src ?或者这实际上是最好的解决方案?

Edit2: Is there any other, better way to do it, instead of plain html src? Or this is the best solution actually?

推荐答案

[...] =...... 用于对象绑定 ... ={{...}}用于绑定字符串插值。如果要绑定字符串,则无关紧要。如果要绑定对象或数组值,则需要使用`[...] =...。除此之外,这完全取决于你。

[...]="..." is for object binding ...="{{...}}" is for binding with string interpolation. If you want to bind a string it doesn't matter what you use. If you want to bind an object or array value you need to use `[...]="...". Besides that it's entirely up to you.

<img class="logo" src="../app/media/appLogo.png">

根本与Angular2无关,那就是纯HTML(没有 [ ] 且没有 {{}} )。如果你在 src 附近添加 [] ,那么Angular会处理 ../ app / media /appLogo.png 作为表达式,但显然不是。

Is not related to Angular2 at all, thats just plain HTML (no [] and no {{}}). If you add [] around src, then Angular will treat ../app/media/appLogo.png as an expression, which it clearly isn't.

使用DomSanitizer时 [src] =...是必需的,因为该值不再是字符串,而是一个对象并使用 {{}} 将其字符串化无效的方式。

When DomSanitizer is used [src]="..." is mandatory because the value no longer is a string, but an object and using {{}} would stringify it in an invalid way.

这篇关于在Angular中使用什么更好 - src或[src]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆