Angular js - isImage( ) - check if it's image by url

前端 未结 2 575
心在旅途
心在旅途 2020-12-08 15:57

Is it possible to check if by a given url the image exists and it\'s an image resource ?

for example:

angular.isImage(\'http://asd.com/asd/asd.jpg\')         


        
2条回答
  •  悲哀的现实
    2020-12-08 16:49

    You can use ng-src

    
    

    Another way is you check if the it exists using the http module.

    var app = angular.module('myapp', []).run(function($http){
      $http.get('http://asd.com/asd/asd.jpg',
        //success
        function(data){
    
        };
    });
    

    Update:

    HTML

    JS

    var app = angular.module('app', []);
    
    app.directive('isImage', function() {
        return {
            restrict: 'A',
            link: function(scope, element, attrs) {
                element.bind('load', function() {
                    alert('image is loaded');
                });
            }
        };
    });
    
    app.controller('Ctrl', function($scope) {
        $scope.src ="http://asd.com/asd/asd.jpg";
    });
    

提交回复
热议问题