你好,我想知道如何遍历分配给$ scope的API结果。结果显示未定义
我的HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Api_combine</title>
<script src="./js/angular.js"></script>
</head>
<body ng-app="myApp" ng-controller="mainCtrl">
{{ someFunction() }}
<script src="./scripts/app.js"></script>
<script src="./scripts/apiS.js"></script>
</body>
</html>
我的app.js代码:
let app = angular.module('myApp', []);
app.controller('mainCtrl', function($scope, apiS) {
apiS.getTodos().then(res => $scope.todos = res);
$scope.someFunction = () => {
for (let x = 0; x < $scope.todos.length; x++) {
const todo = $scope.todos[x];
if (todo.userId == 1 && todo.id == 2) {
return "found!!!"
}
}
}
});
我的api服务代码:
app.service('apiS', function($http) {
this.getTodos = function() {
return $http.get('https://jsonplaceholder.typicode.com/todos');
}
});
我想遍历$ scope.todos并将所需结果放入DOM。
但是在控制台中,它会说undefined
如何正确执行此操作?
答案 0 :(得分:0)
我在res之后添加了.data。
apiS.getTodos().then(res => $scope.todos = res.data);