What's better to use in Angular - src or [src]

前端 未结 3 1169
自闭症患者
自闭症患者 2020-12-20 18:17

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

Edit: I have follow

3条回答
  •  鱼传尺愫
    2020-12-20 18:26

    [...]="..." 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.

    
    

    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.

    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.

提交回复
热议问题