简体   繁体   中英

Angularjs: Pictures won't load

UPDATE: It works in Firefox, but not in Chrome! Any ideas???

I am following a tutorial that creates a simple list of Smartphones/Tablets with thumbnails. http://docs.angularjs.org/tutorial/step_07

The Problem: After I did step 7, the thumbnails won't load anymore.

It actually was working the step before, but I can't figure out what I did wrong. If I call the image URLs in the URL bar directly, it works. But Angular can't do it, somehow. I don't get any errors in Chrome's JavaScript console either.

The Thumbnails are located under /app/img/phones/ (eg /app/img/phones/dell-streak-7.0.jpg)

Below are the files I think of that are relevant. If you need more intel, let me know. Thanks a lot!

/app/index.html

<!doctype html>
<html lang="en" ng-app="phonecatApp">
<head>
  <meta charset="utf-8">
  <title>My HTML File</title>
  <link rel="stylesheet" href="css/app.css">
  <link rel="stylesheet" href="css/bootstrap.css">

  <!-- Angular imports -->
  <script src="lib/angular/angular.js"></script>
  <script src="lib/angular/angular-route.js"></script>

  <!-- My imports -->
  <script src="js/app.js"></script>
  <script src="js/controllers.js"></script>
</head>
<body>

    <div ng-view></div>

</body>
</html>

/app/partials/phone-list.html

<div class="container-fluid">
  <div class="row-fluid">
    <div class="span2">
      <!--Sidebar content-->

          Search: <input ng-model="query">
      Sort by:
      <select ng-model="orderProp">
        <option value="name">Alphabetical</option>
        <option value="age">Newest</option>
      </select>

    </div>
    <div class="span10">
      <!--Body content-->

      <ul class="phones">
        <li ng-repeat="phone in phones | filter:query | orderBy:orderProp" class="thumbnail">
          <a href="#/phones/{{phone.id}}" class="thumb"><img ng-src="{{phone.imageUrl}}"></a>
          <a href="#/phones/{{phone.id}}">{{phone.name}}</a>
          <p>{{phone.snippet}}</p>
        </li>
      </ul>

    </div>
  </div>
</div>

/app/js/app.js

'use strict';

/* App Module */
var phonecatApp = angular.module('phonecatApp', [
    'ngRoute', 
    'phonecatControllers' 
]);

phonecatApp.config(['$routeProvider',
    function($routeProvider) {
        $routeProvider.
            when('/phones', {
                templateUrl: 'partials/phone-list.html',
                controller: 'PhoneListCtrl'
            }).
            when('/phones/:phoneId', {
                templateUrl: 'partials/phone-detail.html',
                controller: 'PhoneDetailCtrl'
            }).
            otherwise({
                redirectTo: '/phones'
            });

    }
]);

/app/js/controllers.js

'use strict';

/* Controllers */

var phonecatControllers = angular.module('phonecatControllers', []);

phonecatControllers.controller('PhoneListCtrl', ['$scope', '$http', 
    function ($scope, $http) {
        $http.get('phones/phones.json').success(function(data) {
            $scope.phones = data;
        })

        $scope.orderProp = 'age'
    }
]);


phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', 
    function($scope, $routeParams) {
        $scope.phoneId = $routeParams.phoneId
    }
]);

Try without the {{}} around the url in the ng-src attribute.

The ng-src directive takes an expression as the parameter so you don't need to use the binding syntax {{}} . The binding syntax basically is a way to tell angular to expect an expression.

The documentation on http://docs.angularjs.org/api/ng/directive/ngSrc isn't absolutely crystal clear because they use the binding syntax within the expression.

<img ng-src="http://www.gravatar.com/avatar/{{hash}}"/>

I have found the solution.

It worked in Firefox as-is, but not in Chrome. Clearing the browsing data didn't help at all. The problem was caused by AngularJS Batarang in some way I don't know.

So I have disabled the Chrome extension AngularJS Batarang and it instantly worked.

See report here: https://github.com/angular/angularjs-batarang/issues/107

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM