我会尽可能清楚地解释我的问题。我第一次尝试在我的Laravel项目中使用angularJs
控制器只是从数据库中获取上传的照片
public function index()
{
JavaScript::put([
'photos' => Photo::all()
]);
return view('pages.protected.photos.index');
}
我使用laracast中的PHP Vars-To-Js包(这就是您看到的JavaScript外观)将我的PHP照片对象传递给我的JavaScript。但这不是问题,效果很好。现在,多亏了这个软件包,我可以通过将照片对象直接放入我的角度控制器
$scope.photoList = photos;
到目前为止还不错。现在我想实际显示图像,为了得到它,我设置了一个div
<div class="row" ng-repeat="photo in photoList">
div内部有
<img ng-src="@{{ photo.path }}">
问题是:照片路径类似于photo/picture.png,其中photo是公共目录中的一个文件夹,我试图显示图像(照片)的页面位于前缀为/admin
的route group
中(末尾类似于www.myapp.com/admin/photos)。当我试图显示图像时,我会收到404错误,因为它在www.myapp.com/admin/photo/picture.png
而不是www.myapp.com/photo/picture.png
中查找照片src
我的问题是:如何"强制"使用基本url来搜索正确的照片路径?
您需要在控制器中添加一个方法来获取baseURL。
角度控制器
$scope.getImageSourceFromPhotoPath = function (photoPath) {
return window.location.protocol + '//' + window.location.host + '/' + photoPath;
}
模板
<div class="row" ng-repeat="photo in photoList">
<img ng-src="getImageSourceFromPhotoPath(photo.path)">
</div>