我第一次尝试从API获取帐户列表并将其显示在简单列表中。该API是在一个单独的域上。到目前为止,我有我的app.js如何通过使用Angular的API获取数据?
var myApp = angular.module("myApp", []);
myApp.controller("myController", function($scope){
// when landing on the page, get all todos and show them
$http.get('http://api.example.com/accounts')
.success(function(data) {
$scope.accounts = data;
console.log(data);
})
.error(function(data) {
console.log('Error: ' + data);
});
});
以下内容,我的index.html
<!DOCTYPE html>
<html>
<script src="bower_components/angular/angular.js"></script>
<script src="app/app.js"></script>
<body ng-app="myApp" ng-controller="myController">
<ul ng-repeat="account in accounts">
<li >
{{ account.name }}
</li>
</ul>
</body>
和样品响应。
[
{
"name": "Discover",
"_id": "55d532da7fc30ff81f000008",
"__v": 0
},
{
"name": "Citi",
"_id": "55d6b9967fc30ff81f000009",
"__v": 0
}
]
我似乎没有得到它的工作。我得到的是一个空白页和控制台显示此错误中铬:
'mainController()' is not a function, got undefined
在控制台中的任何错误?你是否也在使用最新版的'angular'?如果是这样,你需要在应用程序中注入'ngRoute'依赖项。 :) – AdityaParab
您应该添加$ http作为控制器中的函数控制器 – Ayoub
是的,我正在运行最新的角度。仍然找出错误日志,因为我切换到了新的IDE。 – dikshant