(Plunkr)
我有一个页面,用户输入用户名,网站会获取一些元数据,包括Gravatar ID。然后,它使用ID显示头像,但是,尽管存在img
标记,其中包含正确的URL,并且浏览器的开发者控制台中没有错误,图像也不会显示。
我还可以看到正在进行的HTTP调用,并返回200。
如果我在Firefox中打开Plunkr,我可能会出现引用错误:
ReferenceError: angular is not defined
http://run.plnkr.co/plunks/4tGAFuqjfqn9hYUy1Xoa/app.js
Line 1
在IE和Safari中,它按预期工作,只有Chrome失败且没有错误。
<!DOCTYPE html>
<html ng-app="gitHubViewer">
<head lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<script src="script/angular/app.js"></script>
<script src="script/angular/controllers/main-controller.js"></script>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div ng-controller="MainController">
<div>
<h2>{{user.name}}</h2>
<div>Username: {{user.login}}</div>
<div>Gravatar ID: {{user.gravatar_id}}</div>
<img ng-src="http://www.gravatar.com/avatar/{{user.gravatar_id}}" />
</div>
<form name="searchUser">
<input type="search" placeholder="Username to find." ng-model="username" />
<input type="submit" value="Search" ng-click="search(username)"/>
</form>
</div>
</body>
</html>
主控制器:
(function (app) {
var MainController = function ($scope, $http) {
$scope.message = 'Hello World!';
$scope.search = function (username) {
console.log('Username = ' + username);
$http.get('https://api.github.com/users/' + username)
.then(onUserComplete);
};
var onUserComplete = function (response) {
$scope.user = response.data;
};
};
app.controller("MainController", ["$scope", "$http", MainController])
})(gitHubViewer);
app.js
var gitHubViewer = gitHubViewer || angular.module("gitHubViewer", [], function () {
});