Understanding AngularJS ng-src

前端 未结 6 1979
猫巷女王i
猫巷女王i 2020-12-03 09:51

As explained here, the angularjs directive ng-src is used to prevent the browser from loading the resource (e.g. image) before the handlebars get parsed. I\'m currently usin

6条回答
  •  生来不讨喜
    2020-12-03 10:41

    I hit the same issue also. One thing I noticed is that if the value for ng-src is undefined then no img is fetched. Therefore, I created a utility method to concat two arguments and return a value if and only if both arguments are defined. See below.

    myApp.factory('MyUtil', function() {
        return {
            strConcat: function(str1, str2) {
                return (angular.isDefined(str1) && angular.isDefined(str2)) ? 
                    (str1 + str2) : undefined;
            }
        }
    });
    
    function MyCtrl($scope, $timeout, MyUtil) {
        $scope.MyUtil = MyUtil;
    ...
    }
    

    FIDDLE

提交回复
热议问题