简体   繁体   English

Angular - 高度和宽度的属性绑定不适用于图像

[英]Angular - Property Binding for height & width not working for Image

I am trying to make an example of property binding in Angular.我试图在 Angular 中做一个属性绑定的例子。 I decided to take an image and then put all 3 attributes of it -- width, height, src by property binding.我决定拍摄一张图像,然后通过属性绑定将它的所有 3 个属性——宽度、高度、src。 To my surprise, though there was no error, and image is being rendered by URL (no error in url) but still the width and height are being rendered as zero (0).令我惊讶的是,虽然没有错误,并且图像正在由 URL 呈现(在 url 中没有错误),但宽度和高度仍然被呈现为零 (0)。 Why this?为什么这个? As far as I know Image takes height and width as its properties.据我所知,Image 将高度和宽度作为其属性。 Then where is the problem?那么问题出在哪里?

abc.component.html abc.component.html

//This does not work - gives height:0px, width:0px
<img [width]="'100px'" [height]="'100px'" [src]="'./assets/img/hermione.jpg'">

//This works - renders image, with height:100px, width: 100px
<img width="100px" height="100px" [src]="'./assets/img/hermione.jpg'">

Can someone explain by the strange behavior of why the second scenario runs well, but in first though images gets rendered but never seen (as height:0px, width:0px)?有人可以解释为什么第二种情况运行良好的奇怪行为,但首先虽然图像被渲染但从未见过(如高度:0px,宽度:0px)?

Error:错误:

在此处输入图像描述

Also, (as per Pronoy Sarkar - see answer below)另外,(根据 Pronoy Sarkar - 请参阅下面的答案)

//This also works
<img [attr.width]="'100px'" [attr.height]="'100px'" [src]="'./assets/img/hermione.jpg'">

Now, question is why simple [height] and [width] does not work?现在,问题是为什么简单的 [height] 和 [width] 不起作用? Afterall, we never did [attr.src] ?毕竟,我们从来没有做过[attr.src]

试试[attr.width][attr.height]

If you have a look at the Attribute List here on HTML attribute reference - Attribute List Page on MDN , it says that width and height are attributes and NOT Native Properties . 如果您在HTML属性引用--MDN上的属性列表页面上查看属性列表 ,它会说widthheight是属性而不是本机属性

If you then go over to Attribute Binding section of Template Syntax Fundamentals Section, it states that: 如果您转到模板语法基础部分的属性绑定部分,它会声明:

As the message says, the element does not have a colspan property. 如消息所示,元素没有colspan属性。 It has the "colspan" attribute, but interpolation and property binding can set only properties, not attributes. 它具有“colspan”属性,但插值和属性绑定只能设置属性,而不能设置属性。

You need attribute bindings to create and bind to such attributes. 您需要属性绑定来创建和绑定到这些属性。

On the similar lines, width and height aren't Native Properties of an img tag. 在类似的行上, widthheight不是img标签的Native Properties。 They are attributes. 它们是属性。

Hence you can't set width and height using the property binding syntax( [width] / [height] ). 因此,您无法使用属性绑定语法( [width] / [height] )设置宽度和高度。 You'll have to use the Attribute Binding Syntax instead. 您将不得不使用属性绑定语法。

PS: src was also present in the Attribute List Page on MDN page. PS: src也存在于MDN页面的属性列表页面中。 But since in the description, there wasn't a mention of it as a property, I figured it was a native property. 但是由于在描述中没有提到它作为财产,我认为它是一个本地财产。

You can use Attribute binding for html attribute :您可以对 html 属性使用属性绑定

[attr.width]="'100px'"
[attr.height]="'100px'"

or或者

[attr.width.px]="widthProp"
[attr.height.px]="heightProp"

or或者

[attr.width.%]="widthProp"
[attr.height.%]="heightProp"

or you can use Styling binding for css style :或者您可以对 CSS 样式使用样式绑定

[style.width]="'100px'"
[style.height]="'100px'"

or或者

[style.width.px]="widthProp"
[style.height.px]="heightProp"

or或者

[style.width.%]="widthProp"
[style.height.%]="heightProp"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有属性绑定的Angular 2动画不起作用 - Angular 2 animation with property binding not working 使用 TypeScript 获取 Angular 中图像的高度和宽度 - Getting the height and width of an image in Angular using TypeScript Angular4中SVG图像的自定义高度和宽度 - Custom height and width of SVG image in angular4 图像尺寸问题,宽度和高度不起作用 - Image size issue, width and height not working 按钮属性绑定“禁用”在角度4中不起作用 - button property binding “disable” not working in angular 4 “取消选中所有复选框”不使用角度属性绑定 - “Deselect all checkboxes” not working with angular property binding 属性高度100%在Angular 7 + Electron 4中不起作用 - Property height 100% not working in Angular 7 + Electron 4 尝试在具有动态宽度和高度的 guillotine.js 中显示图像。 固定宽度和高度工作正常 - Trying to display image in guillotine.js with dynamic width & height. Working fine with fixed width & height canvas drawimage在具有CSS高度和宽度的图像上无法正常工作 - canvas drawimage is not working properly on image with css height and width 获取图像宽度和高度在 50% 的时间在 vue js 中工作 - Get image width and height working 50% of the time in vue js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM