$http服务angular不工作
$http service angular not working
我在控制器中使用Angular的$http服务向web服务器发出http请求,以获取app/phones/phones.json文件中的数据。但手机列表不会通过index.html显示
app/js/controllers.js:
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', ['$scope', '$http', function($scope, $http) {
$http.get('phones/phones.json').success(function(data) {
$scope.phones = data;
});
$scope.orderProp = 'age';
}]);
///////////index.html:
<html ng-app="phonecatApp" ng-controller="PhoneListCtrl">
<head>
<meta charset="utf-8">
<title>Google Phone Gallery:{{query}}</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
<link rel="stylesheet" href="css/app.css">
<script src="bower_components/angular/angular.js"></script>
<script src="js/controllers.js"></script>
</head>
<body >
<div class="container-fluid">
<div class="row">
<div class="col-md-2">
<!--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="col-md-10">
<!--Body content-->
<ul class="phones">
<li ng-repeat="phone in phones| filter:query|orderBy:orderProp">
<span> {{phone.name}} </span>
<p>{{phone.snippet}}</p>
</li>
</ul>
</div>
</div>
</div>
<div style="padding:100px">
Additional
<p> Total number of phones:{{phones.length}}</p>
<p>Hello,{{name}}!</p>
<table>
<tr><th>row number</th></tr>
<tr ng-repeat="i in [0, 1, 2, 3, 4, 5, 6, 7]"><td>{{i + 1}}</td><td>{{i + 1}}</td><td>{{i + 1}}</td><td>{{i + 1}}</td><td>{{i + 1}}</td><td>{{i + 1}}</td><td>{{i + 1}}</td><td>{{i + 1}}</td></tr>
</table>
</div>
</body>
</html>
///////////////////////
这是json文件:
[
{
"age": 0,
"id": "motorola-xoom-with-wi-fi",
"imageUrl": "img/phones/motorola-xoom-with-wi-fi.0.jpg",
"name": "Motorola XOOM'u2122 with Wi-Fi",
"snippet": "The Next, Next Generation'r'n'r'nExperience the future with Motorola XOOM with Wi-Fi, the world's first tablet powered by Android 3.0 (Honeycomb)."
},
{
"age": 1,
"id": "motorola-xoom",
"imageUrl": "img/phones/motorola-xoom.0.jpg",
"name": "MOTOROLA XOOM'u2122",
"snippet": "The Next, Next Generation'n'nExperience the future with MOTOROLA XOOM, the world's first tablet powered by Android 3.0 (Honeycomb)."
},
{
"age": 2,
"carrier": "AT&T",
"id": "motorola-atrix-4g",
"imageUrl": "img/phones/motorola-atrix-4g.0.jpg",
"name": "MOTOROLA ATRIX'u2122 4G",
"snippet": "MOTOROLA ATRIX 4G the world's most powerful smartphone."
},
{
"age": 3,
"id": "dell-streak-7",
"imageUrl": "img/phones/dell-streak-7.0.jpg",
"name": "Dell Streak 7",
"snippet": "Introducing Dell'u2122 Streak 7. Share photos, videos and movies together. It'u2019s small enough to carry around, big enough to gather around."
}]
请参阅此处http://plnkr.co/edit/17K7bH7IQ1RgwY6JZMiN?p=preview
更改您的呼叫,使您可以查看$get不起作用的原因
$http.get('phones.json').then(function(response) {
$scope.phones = response.data;
},
//failed $get call
function(a, b, c) {
alert("can't get data")
console.log(a);
console.log(b);
console.log(c);
});
您的问题可能是必须允许在web.config中加载json文件:
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
我也遇到过类似的问题,并用这个解决了它。希望能有所帮助。:)
相关文章:
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- 为什么不是'我的函数在解析云代码中工作吗?当我在Angular和Express中测试时,它是有效的
- 注入工厂时,Angular停止工作
- Angular Js布线工作不正常
- 代码在angular中未按预期工作
- Angular JS-视图不工作
- angular js应用程序不工作
- grunt serve:dist时找不到Angular.js模块,但grunt serve工作正常
- 带有Angular指令的HTML;附加时无法工作
- 2D数组在Angular JS 1.2.x中工作,而不是在1.3.x中工作
- 如何使Angular JS控制器与指令一起工作
- $scope$Angular中的手表;我不在爱奥尼亚/科尔多瓦工作
- 让typescript与facebook登录为Angular 2项目工作
- 提前输入以在 Angular 模板中工作
- 最基本的 Angular 应用程序无法正常工作
- Angular 的新手 - 在 eclipse 中的设置无法正常工作
- 与 Angular 1.5 的单向数据绑定类似于双向工作
- 使用 Angular 和 UI-bootstrap 对模态事件绑定无法工作的指令
- Angular ng sortable-如果我在一个栏中添加了一个搜索框,则拖放在两列之间无法正常工作
- 我的$asyncValidator不工作-Angular Javascript