Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
防止img在angularjs中加载_Angularjs - Fatal编程技术网

防止img在angularjs中加载

防止img在angularjs中加载,angularjs,Angularjs,我有一个带图像的div: <div ng-if="showIt"> <img src="{{filepath}}/{{filename}}"> <!-- ... and a bunch other other elements --> </div> 问题是{{filepath}}如果showIt为false,{{filename}}将为null 但是,我总是在控制台中看到404错误,因为我认为Angularjs正在渲染整个DO

我有一个带图像的div:

<div ng-if="showIt">
    <img src="{{filepath}}/{{filename}}">
    <!-- ... and a bunch other other elements -->
</div>

问题是
{{filepath}}
如果
showIt
为false,
{{filename}}
将为null

但是,我总是在控制台中看到404错误,因为我认为Angularjs正在渲染整个DOM,然后删除
ng(如果
的计算结果为false)的位。这意味着即使
showIt
为false,浏览器也将始终请求不存在的图像

有没有办法阻止DOM创建整个div块,这样浏览器就永远不会请求不存在的图像?

使用ng src:

<img ng-src="{{filepath}}/{{filename}}">

您看到的404错误不是由ng if引起的。这是由于浏览器试图在angular计算角度表达式之前加载图像造成的
ng src
仅在计算表达式后将
src
属性添加到图像,避免对
{{filepath}/{{filename}}
使用ng src:

<img ng-src="{{filepath}}/{{filename}}">

您看到的404错误不是由ng if引起的。这是由于浏览器试图在angular计算角度表达式之前加载图像造成的
ng src
仅在计算表达式后将
src
属性添加到图像,避免对
{{filepath}/{{filename}}
的HTTP请求