Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如果生成的图像url在angular中不可用,如何返回默认图像url_Angularjs - Fatal编程技术网

Angularjs 如果生成的图像url在angular中不可用,如何返回默认图像url

Angularjs 如果生成的图像url在angular中不可用,如何返回默认图像url,angularjs,Angularjs,我是新来的 $scope.imageUrl[0] = 'images/' + $scope.repos[i].name + '.png'; $scope.imageUrl[1] = 'images/placeholder.png'; $scope.repos[i].imgSrc = (Utils.isImage($scope.imageUrl[0])) ? $scope.imageUrl[0] : $scope.imageUrl[1]; Utils.isImage()来自以下工厂: app.

我是新来的

$scope.imageUrl[0] = 'images/' + $scope.repos[i].name + '.png';
$scope.imageUrl[1] = 'images/placeholder.png';

$scope.repos[i].imgSrc = (Utils.isImage($scope.imageUrl[0])) ? $scope.imageUrl[0] : $scope.imageUrl[1];
Utils.isImage()来自以下工厂:

app.factory('Utils', function($q) {
return {
  isImage: function(src) {

    var deferred = $q.defer();

    var image = new Image();
    image.onerror = function() {
      deferred.resolve(false);
      return false;
    };
    image.onload = function() {
      deferred.resolve(true);
      return true;
    };

    image.src = src;

    return deferred.promise;
   }
 };
});
图像url由api响应对象生成。我正在使用Utils.isImage()检查生成的图像url是否存在于服务器上的images文件夹中。 但是(Utils.isImage($scope.imageUrl[0])仍然为真,即使在服务器上找到或未找到该映像。 我怎样才能解决这个问题


Plunker link-

函数返回一个承诺,因此总是
正确的

使用
$q
服务提供异步功能。()

要正确处理承诺,您应至少使用
then()
函数,并为已解决的已拒绝的承诺提供回调

Utils.isImage($scope.imageUrl[0])).then(
   function() {
      // Here is true  
      $scope.repos[i].imgSrc = $scope.imageUrl[0]
   },
   function() {
      // Here is false
      $scope.repos[i].imgSrc = $scope.imageUrl[1]
   }
)

您可以简单地编写如下指令:

myAppModule.directive('noImageIcon', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            var defaultURL = attr.defaultURL || 'images/plaecholder.png';

            attr.$observe('ngSrc', function(newValue) {
                if (!newValue) {
                    element.attr('src', defaultURL);
                } else {
                    var image = new Image();
                    image.onerror = function() {
                         element.attr('src', defaultURL);
                    };
                    image.onload = function() {
                    };

                    image.src = newValue;
                }
            });
        }
    };
});
现在,您的图像html标记将如下所示:

<img ng-repeat="repo in repos" ng-src="images/{{repo.name}}.png" />


这是什么UTIL?我会在服务器端这样做-服务器应该知道它有哪些映像。我如何在控制器中使用该指令?这是指向我的plunker文件的链接-您可以查看一下itI在实现then函数后得到此错误:TypeError:无法在Utils.isImage.then.$scope.repos处设置未定义的属性'imageSrc'。(匿名函数).imageSrc()这是指向我的plunker文件的链接-