Angularjs 如果api的返回类型为“如何显示虚拟图像”;“没有图像”;使用AngualRJ?
我有一个api,如果映像位于对应于该id的服务器上,则返回映像;如果映像不在服务器上,则返回json响应“无映像” 所以,当“无图像”响应出现时,我必须使用angularjs使用dummuy图像 这是我的密码:Angularjs 如果api的返回类型为“如何显示虚拟图像”;“没有图像”;使用AngualRJ?,angularjs,Angularjs,我有一个api,如果映像位于对应于该id的服务器上,则返回映像;如果映像不在服务器上,则返回json响应“无映像” 所以,当“无图像”响应出现时,我必须使用angularjs使用dummuy图像 这是我的密码: server = "http://localhost:8000/" $scope.image = function (id) { return test = server + 'api/image/' + id;
server = "http://localhost:8000/"
$scope.image = function (id) {
return test = server + 'api/image/' + id;
};
HTML代码:
<img width = 100px; height:60px; ng-src="{{image(n.userID)}}" alt="" class="img-responsive"/>
请告诉我,如果“无图像”存在,如何放置虚拟图像?…请参见此处,您可以使用ng show/hide显示正确的图像或“无图像”占位符
<img width = 100px; height:60px; ng-src="{{image(n.userID)}}" alt="" class="img-responsive" ng-show="image(n.userID)!='no image'" />
<img ng-src="http://www.isrj.net/ArticleImage/No_Image.jpg" ng-hide="image(n.userID)!='no image'">
我创建了一个非常简单的Angular指令,每当出现错误时,它都会隐藏img元素
angular.module('SomeModuleName', [])
.directive('hideEmptyImage', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
elem.error(function(err) {
elem.hide();
});
}
};
});
就像我说的,很简单。
然后,对于我想要隐藏的每一张图像,我只想:
<img hide-empty-image ng-src="{{n.userID}}" />
您的html将如下所示:
<img default-image width = 100px; height:60px; ng-src="{{image(n.userID)}}" alt="" class="img-responsive"/>
使用此指令,如果没有图像,则将用默认图像替换它。简单。听说过if…else语法吗-_-是的,我听到了,也尝试了,但不起作用……因为如果图像在数据库中,它会直接显示图像,如果图像不在那里,它会返回json响应……请提供更多代码,因为除了一个简单的
if(id==“No image”)
我不明白问题所在。我使用了我发布的那么多代码……这也不起作用,当那时没有图像时,我得到这个json响应“{“error”:true,“details”:“no image.”,否则api会返回图像本身…我要问的是,当图像在数据库中时,它会返回图像而不是json响应,那么我如何使用if语句…我很困惑…如果我尝试使用$http.get,那么我不会得到任何响应,因为我的页面没有显示任何内容…一切都从页面开始…@PreetySapra你能创建jsbin吗对于您的代码,如果我只能看到您的解决方案的一部分,则很难给出建议
<img default-image width = 100px; height:60px; ng-src="{{image(n.userID)}}" alt="" class="img-responsive"/>