douao1959 2015-11-20 20:31
浏览 56
已采纳

Laravel + AngularJs,使用ng-scr的基本路径而不是当前路径

I'll try to explain my issue as clear as I can. I'm trying to use angularJs in my Laravel project for the very first time.
The controller just fetches the uploaded photos from the database

public function index()
    {
        JavaScript::put([
            'photos' => Photo::all()
        ]);

        return view('pages.protected.photos.index');
    }  

I'm using the PHP-Vars-To-Js package from laracast (that's the JavaScript facade you see) to pass my PHP photos object over to my javascript. But this isn't a problem, it works pretty well. Now, thanks to that package, I can get the photos object right into my angular controller simply by

$scope.photoList = photos;  

So far so good. Now I want to actually display the images and to get that I did set a div

<div class="row" ng-repeat="photo in photoList">

Inside the div there's

<img ng-src="@{{ photo.path }}">  

The problem is: photos path is something like photo/picture.png, where photo is a folder in the public directory and the page where I'm trying to display the images (photos) is in a route group with the /admin prefix (at the end something like www.myapp.com/admin/photos). When I try to display the image I get a 404 error cause it's looking for the photos src in www.myapp.com/admin/photo/picture.png instead of www.myapp.com/photo/picture.png.
My question is: how can I 'force' to use the base url in order to search for the right photo path?

  • 写回答

1条回答 默认 最新

  • dongliechuich10319 2015-11-20 20:38
    关注

    You'll want to add a method in the controller to get the baseURL.

    Angular Controller

    $scope.getImageSourceFromPhotoPath = function (photoPath) {
        return window.location.protocol + '//' + window.location.host + '/' + photoPath;
    }
    

    Template

    <div class="row" ng-repeat="photo in photoList">
        <img ng-src="getImageSourceFromPhotoPath(photo.path)">
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?