图片不被加载在img标签的NG-重复内 [英] Images not being loaded in img tag inside an ng-repeat
问题描述
我的问题:
我想显示的图像的列表,localY坐标存储,使用纳克重复指令,但重新未被显示的图像。
I want to display a list of images, stored localy, using ng-repeat directive, but the images re not being displayed.
AngularJS版本:1.2.22
AngularJS version: 1.2.22
的code:
-
目录结构:
Directory Structure:
myapp/
|--- common/
| |--- images/
| |--- image.png
|
|--- subapp1/
| |--- index.html
| |--- subapp1.js
| |
| |--- controllers/
| | |--- SubApp1Controller.js
| |
| |--- views/
| | |--- imageListView.html
index.html的
index.html
<!DOCTYPE html>
<html lang="en" data-ng-app="SubApp1">
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="page" data-ng-view="">
</div>
<!-- Include required libs, models and controllers -->
<script type="text/javascript" src="../common/libs/angular.min.js"></script>
<script type="text/javascript" src="../common/libs/angular-route.min.js"></script>
<script type="text/javascript" src="../common/libs/angular-touch.min.js"></script>
<!-- Controllers -->
<script type="text/javascript" src="controllers/SubApp1Controller.js"></script>
<!-- App Module (main) -->
<script type="text/javascript" src="subapp1.js"></script>
</body>
</html>
- subapp1.js
// Instantiate the SubApp1 Module.
var subApp1 = angular.module("SubApp1", ["ngRoute", "ngTouch"]);
// Setting Controllers
subApp1.controller("SubApp1Controller",
["$scope", SubApp1Controller]
);
// Define the routes for the module.
subApp1.config(function ($routeProvider) {
$routeProvider.when("/home", {
controller: "SubApp1Controller",
templateUrl: "views/imageListView.html"
});
$routeProvider.otherwise({redirectTo: "/home"});
});
- SubApp1Controller.js
function SubApp1Controller($scope)
{
/**
* Private Method: _run()
* The main function of the SubApp1Controller, where all the magic
* happens.
*/
function _run() {
// this variable will define which style will be loaded on the image list view
$scope.section = "subapp1";
var imageSource;
imageSource = "../common/images/image.png";
// list of media elements to be displayed on the list
$scope.mediaList = [
{
thumbnailPath: imageSource,
imagePath: imageSource,
},
{
thumbnailPath: imageSource,
imagePath: imageSource,
},
{
thumbnailPath: imageSource,
imagePath: imageSource,
},
{
thumbnailPath: imageSource,
imagePath: imageSource,
},
{
thumbnailPath: imageSource,
imagePath: imageSource,
},
];
};
// Runs the main method of this class
_run();
}
- imageListView.html
<div class="grid-background"></div>
<header class="grid-header {{section}}">
<div class="button-right"></div>
</header>
<ul id="grid" class="grid">
<li class="grid-item" data-ng-repeat="media in mediaList">
<img data-ng-src="{{media.thumbnailPath}}" data-src="{{media.imagePath}}"/>
</li>
</ul>
额外的信息:
图像没有被加载并打印在控制台中没有错误消息。
The images are not being loaded and no error message is printed in the console.
我打开Web检查工具,看看有什么是在li元素处理,它没有src属性,参见下文:
I opened the Web Inspector tool to see what was processed in the li element and it did not have the src attribute, see bellow:
<li class="grid-item ng-scope" data-ng-repeat="media in mediaList">
<img data-ng-src="../common/images/image.png" data-src="../common/images/image.png" />
</li>
如果我加入img元素的src属性,它被加载,但我得到一个错误,参见下文:
If I add the src attribute in the img element, it is loaded, but I get an error, see bellow:
<li class="grid-item" data-ng-repeat="media in mediaList">
<img data-ng-src="{{media.thumbnailPath}}"
data-src="{{media.imagePath}}"
src="{{media.thumbnailPath}}"
/>
</li>
<!-- Error message printed in the console -->
GET file:///[ABS_PATH]/%7B%7Bmedia.thumbnailPath%7D%7D net::ERR_FILE_NOT_FOUND
我已经看了很多帖子和有关类似问题的提问/回答,但他们都不为我工作。有没有人有线索?
I have read many posts and questions/answers about similar issues, but none of them worked for me. Does anyone have a clue?
先谢谢了。
推荐答案
有关IMG标记和AngularJS,只使用 NG-SRC 属性。
For IMG tags and AngularJS, use just the ng-src attribute.
<img ng-src="{{media.imagePath}}" />
{{media.imagePath}} 必须包含的URL的形象,绝对或相对的。
{{media.imagePath}} must contain the URL to the image, absolute or relative.
这篇关于图片不被加载在img标签的NG-重复内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!