在日常开发工作中,有很多需求是在一个页面上显示一些图片。有时,图片的地址路径是由客户端的脚本来设置(它有可能是从数据库中获取的)。
这是Angularjs的时代,当我们想使用Angularjs来实现在页面中展示图片,我们会简单使用:<img src=”path of image”>.
如果我们只考虑展示,毫无疑问它没问题,但是在浏览器的控制台中会显示一个 404 (not found) 错误。
为了解决这个问题,我们需要使用ng-Src
。在使用ng-Src
之前,我想给你重现一下这个错误是如何产生的
示例代码如下:
示例源码
Script.js
@H_502_74@
1
2
@H_403_80@
3
4
5
6
7
8
9
10
11
12
|
var
testApp = angular
.module(
"testModule"
,[])
.controller(
"testController"
function
($scope) {
animal = {
name:
"CAT"
color:
"White"
picture:
"/images/cat.jpg"
};
$scope.animal = animal;
});
|
Index.html
12
13
14
15
@H_
403_200@
16
17
18
<
html
ng-app
=
"testModule"
>
head
>
title
></
>
script
src
"scripts/angular.min.js"
script
>
"scripts/js/script.js"
>
</
>
body
>
div
ng-controller
"testController"
>
Name: {{animal.name}}
br
/>
Color: {{animal.color}}
/>
@H_967_ 301@
img
"{{animal.picture}}"
/>
div
>
>
html
>
|