search

Home  >  Q&A  >  body text

angular.js - How to give default values ​​to variables in html pages in angularjs?

Requirement description: There is a myimg variable in the page. I can use $scope.myimg='xxx' in the controller to give it an initial value. But if assignment is not allowed in the control, what about in the img tag? Give it an initial value?

I tried ng-init="myimg:XXX", but in this case the image cannot be displayed
<img ng-src='./img/{{myimg}}.png'>
淡淡烟草味淡淡烟草味2811 days ago598

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-15 16:56:28

    This is the same operation as adding a default value when initializing a value in JavaScript, just use ||.

    <img ng-src='./img/{{myimg || 'default'}}.png'>

    reply
    0
  • Cancelreply