【发布时间】:2017-08-08 09:47:08
【问题描述】:
<div class="col-xs-4 col-sm-4 col-md-4">
{{jsonData[current].profilepic}}
<div ng-if=IsValidImageUrl(jsonData[current].profilepic)>
<img id="pic" ng-src="{{jsonData[current].profilepic}}" alt=""/>
</div>
<div ng-if=!IsValidImageUrl(jsonData[current].profilepic)>
<div id="pic" class="letter">
<div class="circle">{{jsonData[current].firstName.charAt(1)+jsonData[current].lastName.charAt(1)}}</div>
</div>
</div>
</div>
控制器:
app.controller('task1Controller',['$scope', 'taskFactory', '$state', 'imageTestService', function($scope, taskFactory, $state, imageTestService){
$scope.taskData = {};
$scope.current = 0;
taskFactory.get().then(function(response){
$scope.jsonData = response.data.data.resultCareGivers;
});
$scope.IsValidImageUrl = function(url){
return imageTestService.IsValidImageUrl(url); //Error here
};
$scope.viewDetails = function(){
$state.go('view-details', {details: $scope.jsonData[$scope.current]});
};
$scope.back = function(){
$scope.current = ($scope.current !== 0 ? $scope.current - 1 : 0);
};
$scope.next = function(){
$scope.current = ($scope.current !== $scope.jsonData.length-1 ? $scope.current + 1 : $scope.jsonData.length-1);
};
}]);
图像测试服务:
app.service('imageTestService', function($q){
this.IsValidImageUrl = function(url){
var deferred = $q.defer();
if(url != null && url != ""){
var img = new Image();
img.onerror = function() { deferred.resolve(false); };
img.onload = function() { deferred.resolve(true); };
img.src = url;
return deferred.promise;
}
};
});
控制台中的错误堆栈:
达到 10 次 $digest() 迭代。中止! 观察者在最后 5 次迭代中触发: [[{"msg":"IsValidImageUrl(jsonData[current].profilepic)","newVal":{"$$state":{"status":0}},"oldVal" :{"$$state":{"status":0}}}],[{"msg":"IsValidImageUrl(jsonData[current].profilepic)","newVal":{"$$state":{" status":0}},"oldVal":{"$$state":{"status":0}}}],[{"msg":"IsValidImageUrl(jsonData[current].profilepic)","newVal" :{"$$state":{"status":0}},"oldVal":{"$$state":{"status":0}}}],[{"msg":"IsValidImageUrl(jsonData[当前].profilepic)","newVal":{"$$state":{"status":0}},"oldVal":{"$$state":{"status":0}}}],[ {"msg":"IsValidImageUrl(jsonData[current].profilepic)","newVal":{"$$state":{"status":0}},"oldVal":{"$$state":{"状态":0}}}]]
更新 2:
app.service('imageTestService', function(){
this.IsValidImageUrl = function(url){
var result = {};
if(url != null && url != ""){
var img = new Image();
img.onerror = function() { result.val = true };
img.onload = function() { result.val = false };
return result;
}
};
});
但在控制台中仍然出现同样的错误。
更新 3:
app.service('imageTestService', function(){
this.IsValidImageUrl = function(url){
var result = {
val :false
};
this.img = new Image();
this.img.onabort = function() { result.val = false };
this.img.onerror = function() { result.val = false };
this.img.onload = function() { result.val = true };
return result.val;
};
});
在更新 3 中,无论我做什么,总是会调用 image.onabort()。
它在函数调用本身抛出错误fails to load resource:
$scope.IsValidImageUrl = function(url){ //Failed to load resource error
return imageTestService.IsValidImageUrl(url);
};
【问题讨论】:
-
你有什么问题?
-
你为什么在服务中使用 $q?
-
Vivz 是对的,你应该在 IsValidImageUrl 函数中返回布尔值。
-
@GauravSrivastava 我如何返回布尔值。我认为 image.onerror 或 image.onload 是异步的
标签: javascript angularjs