【发布时间】:2016-04-20 11:09:36
【问题描述】:
为了在图像不实际存在时显示默认图像(而不是损坏的图像),我使用了这段代码:
<img ng-show="order.img" ng-src="{{order.img | fPath}}" onerror="this.style.display='none';"/>
<img ng-hide="order.img" ng-src="{{'default.png' | imgPath}}" />
这里的fPath、imgPath 是在图像名称后附加前缀的过滤器。
大多数时候它运行良好(图像被渲染)。但是,在某些情况下,图像不会显示。在这种情况下,角度 sn-p 被翻译成这个 html(注意第一个 img 标签中的style="display: none;"):
<img ng-show="order.img" ng-src="http://xx.s3.amazonaws.com/o/img1.jpg" onerror="this.style.display='none'" src="http://xx.s3.amazonaws.com/o/img1.jpg" style="display: none;">
<img ng-hide="order.img" ng-src="http://xx.s3.amazonaws.com/default.png" class="ng-hide" src="http://xx.s3.amazonaws.com/default.png" style="">
问题:这可能是什么原因?以及可能的解决方法? onerror 是否比预期的更早被触发?
PS:http://xx.s3.amazonaws.com/o/img1.jpg 是一个有效的图片路径并且可用。
更新:这是定义应用程序/路由/控制器的方式
路线
t_app.config(function ($stateProvider, $urlRouterProvider, $httpProvider) {
// ...
$stateProvider.state('ref', {
url: '/orders/:ref',
templateUrl: 'views/order.htm',
controller: 'orderController'
});
// ...
});
控制器
t_app.controller('orderController', ['$scope', '$stateParams', function($scope, $stateParams) {
// Load the order (this step takes a while)
}]);
【问题讨论】:
-
你是如何声明你的 Angular 应用和控制器的?可能是在加载控制器之前呈现了您的页面。
-
添加了更多细节。你的第二个评论是否与
onerror迟早被触发有关? -
order 变量是远程调用加载的吗?还是它有中间值(比如,在变成“xx.s3.amazonaws.com/o/img1.jpg"”之前,“xx.s3.amazonaws.com”)
-
$scope.order仅在远程调用后填充。直到那时它才出现。
标签: angularjs angularjs-filter onerror