How to use img ng-src in AngularJS on android phon

2019-09-01 00:47发布

The most relevant Q & A I've found is from google groups and I've read the AngularJS docs for ngSrc.

I'm trying to use Angular UI Bootstrap - Carousel in my app and I noticed the demo uses absolute URLs for slides like this: image: 'http://placekitten.com/' + newWidth + '/300'.

It works on my localhost (mac) but I don't think images (slides) display on my android google nexus phone because I only use relative image paths like this: image: '/images/help/hello-1.jpg'.

How can I use relative image paths with ng-src directive without having http:// on an android phone? Is using $location a possible solution (from this question)?

My html:

<div ng-controller="helloCtrl" class="slide-container">
  <div>
    <carousel interval="myInterval">
      <slide ng-repeat="slide in slides" active="slide.active">
        <img ng-src="{{slide.image}}" style="margin:auto;">
        <div class="carousel-caption">
          <h3>{{slide.headline}}</h3>
          <p>{{slide.text}}</p>
          <a ng-href="mailto:help@help.com?subject=help-enquiry" class="menu_button"><p>Contact Us</p></a>
        </div>
      </slide>
    </carousel>
  </div>
</div>

my js:

'use strict';

var app = angular.module('helloApp');

app.controller('helloCtrl', function ($scope) {
  $scope.slides = [
    {
      image: '/images/hello-1.jpg',
      headline: 'Help1',
      text: 'Lorem ipsum dolor sit amet, consectetur adipisic.'
    },
    {
      image: '/images/hello-2.jpg',
      headline: 'Help2',
      text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do.'
    }
  ];
});

1条回答
▲ chillily
2楼-- · 2019-09-01 01:20

For some reason I could not use a file path with several directories like this image: '/images/hello-1.jpg' in my js.

new html:

...
<img ng-src="images/{{slide.image}}" style="margin:auto;">
...

new js:

...
    {
      image: 'hello-1.jpg',
      ...
    },
    {
      image: 'hello-2.jpg',
      ...
    }
  ...
查看更多
登录 后发表回答