最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular img src console error - Stack Overflow

programmeradmin0浏览0评论

I'm using ng-repeat to print all the images from desired folder, and those images are in <a> because I'm using fancyBox.

Here's an example of controller:

var ParentCtrl = function ($scope) {
    $scope.getTimes=function(n){ // for the ng-repeat
        return new Array(n);
    }; 
};

app.controller('projectController', ['$scope','$injector', function($scope, $injector) {
    $injector.invoke(ParentCtrl, this, {$scope: $scope});

    $scope.title = 'project';
    $scope.image_url = 'img/project/';
    $scope.image_num = 14; //image count -> [0.jpg, 1.jpg, 2.jpg, ..., 13.jpg]
}]);

And the template:

<a href="" class="fancybox" rel="project-gallery"
    data-ng-repeat="t in getTimes(image_num) track by $index" 
    data-ng-href="{{image_url+($index)+'.jpg'}}">
        <img src="{{image_url+($index)+'.jpg'}}">
</a>

And this code works fine, it shows all the 14 images. However, I'm getting this error in the console:

GET http://localhost/projects/project-name/%7B%7Bimage_url+($index)+'.jpg'%7D%7D 404 (Not Found)

How to fix that error?

I'm using ng-repeat to print all the images from desired folder, and those images are in <a> because I'm using fancyBox.

Here's an example of controller:

var ParentCtrl = function ($scope) {
    $scope.getTimes=function(n){ // for the ng-repeat
        return new Array(n);
    }; 
};

app.controller('projectController', ['$scope','$injector', function($scope, $injector) {
    $injector.invoke(ParentCtrl, this, {$scope: $scope});

    $scope.title = 'project';
    $scope.image_url = 'img/project/';
    $scope.image_num = 14; //image count -> [0.jpg, 1.jpg, 2.jpg, ..., 13.jpg]
}]);

And the template:

<a href="" class="fancybox" rel="project-gallery"
    data-ng-repeat="t in getTimes(image_num) track by $index" 
    data-ng-href="{{image_url+($index)+'.jpg'}}">
        <img src="{{image_url+($index)+'.jpg'}}">
</a>

And this code works fine, it shows all the 14 images. However, I'm getting this error in the console:

GET http://localhost/projects/project-name/%7B%7Bimage_url+($index)+'.jpg'%7D%7D 404 (Not Found)

How to fix that error?

Share Improve this question edited Nov 9, 2014 at 21:34 Vucko asked Nov 9, 2014 at 21:24 VuckoVucko 20.8k12 gold badges59 silver badges108 bronze badges 1
  • 2 check this: docs.angularjs.org/api/ng/directive/ngSrc – Sebastian Piu Commented Nov 9, 2014 at 21:28
Add a comment  | 

2 Answers 2

Reset to default 21

This is what you are looking for: https://docs.angularjs.org/api/ng/directive/ngSrc It's because browser tries to fetch the image withsrc you provided. If you use ng-src, angular will take care of waiting until the expression is compiled, and then append src to the <img> element.

In your template, please use data-ng-src as opposed to src. Your new template will become

<a href="" class="fancybox" rel="project-gallery"
    data-ng-repeat="t in getTimes(image_num) track by $index" 
    data-ng-href="{{image_url+($index)+'.jpg'}}">
        <img data-ng-src="{{image_url+($index)+'.jpg'}}">
</a>

See

https://docs.angularjs.org/api/ng/directive/ngSrc

发布评论

评论列表(0)

  1. 暂无评论