Understanding AngularJS ng-src

前端 未结 6 1950
猫巷女王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:31

    You can set the ng-src to an empty string if the data has not been populated yet:

    <div ng-controller="MyCtrl">
      <img data-ng-src="{{ path && 'http://localhost:8081/media/'+path || '' }}" />
    </div>
    

    when path is uninitalized, the condition would short-circuit and go to the or part and set the data-ng-src to '' (empty string), thus not hitting the server.

    0 讨论(0)
  • 2020-12-03 10:35

    Put the whole path inside the $scope variable. That way ng-src will wait until you provide it with the fully resolved path to the image:

    <div ng-controller="MyCtrl">
      <img ng-src="{{ path }}" />
    </div>
    
    function MyCtrl($scope, $timeout) {
        var path = 'https://si0.twimg.com/profile_images/';
        $timeout(function () {
            $scope.path = path + '2149314222/square.png';
        }, 1000);
    };
    

    FIDDLE

    0 讨论(0)
  • 2020-12-03 10:35

    I am sure 100% work

    First you have to make your query like this

    select (select '../Images/'|| T_LANG2_NAME ||'.png' T_LANG2_NAME from T04222_T where T_LOG_ID = T04220.T_C_STATUS) TIMER from T04220 
    where T_PAT_NO = '89004331' group by T_C_STATUS 
    having max(T_ARRIVAL_DATE) = (select max(T_ARRIVAL_DATE) from T04220 where T_PAT_NO = '89004331');) then you write Code for Controller like this ( if (scope.T_PAT_NO) {
                    debugger;
                    $http({
                            method: 'POST',
                            url: '/T04205/GetTimerImage',
                            data: JSON.stringify({ PatientCode: scope.T_PAT_NO })
                        }).
                        success(function (data) {
                            debugger;
                            var newDataJSON = JSON.parse(data);
    
                            scope.TIMER = newDataJSON[0].TIMER;
    
                        });
    

    then html code like this

    <input id="imTimer" type="image" src="{{TIMER}}" style="width: 80px;height: 80px" />
    
    0 讨论(0)
  • 2020-12-03 10:38

    In the latest, you can evaluate it like this:

    ng-src="{{ methodThatReturnsString() }}"
    
    0 讨论(0)
  • 2020-12-03 10:40

    Info by example


    Let's take this blogitem directive. The examples above already show you how to set a default value.


    HTML :

    <blogitem ng-repeat="item in items" 
              bg-src="{{ item.image }}" 
              caption="{{ item.title }}"/>
    

    JS :

    .directive( 'blogitem', function()
    {
        return {
            restrict    : 'E',
            templateUrl : 'js/app/directives/blogitem.html',
            replace     : true,
            // pass these two names from attrs into the template scope
            scope       : {
                intro : '@',
                bgSrc : '@'
            }
        }
    } )
    

    HTML template :

    <article>
        <img ng-src="{{ bgSrc }}"/>
        <p>{{ intro }}</p>
    </article>
    

    Hopefully it helps by your understanding of the ng-src.

    0 讨论(0)
  • 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.

    <div ng-controller="MyCtrl">
      <img ng-src="{{MyUtil.strConcat('http://localhost:8081/media/', path)}}" />
    </div>
    
    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

    0 讨论(0)
提交回复
热议问题