Я хотел бы передать данные из компонента todos в компонент todo и отобразить все элементы в виде списка.Я загружаю данные из сервиса и помещаю их в todos.controller
.В todos.component
я использую bindings: {todos: '<'}
.В представлении todos.html
он предоставляет данные в виде todos = $ ctrl.todos
.В todo.html
он перебирает todos
и хочет вернуть todo.name
.Эффект: возвращает только 'Todo'
.
todo.service.js
export class TodoService {
constructor($http) {
'ngInject';
this.$http = $http;
}
getTodos() {
return this.$http.get('/api/todos');
}
}
todos.controller.js
class TodosController {
constructor(TodoService) {
'ngInject'
this.TodoService = TodoService;
}
$onInit() {
this.todos = null;
this.TodoService.getTodos().then(response =>
{
this.todos = response.data;
console.log(this.todos);
});
}
}
export default TodosController;
todo.component.js
import template from './todo.html';
import controller from './todo.controller';
import './todo.scss';
let todoComponent = {
bindings: {
todos: '<'
},
template,
controller
};`
export default todoComponent;
todos.html
<h1>Todos</h1>
<ul>
<todo todos="$ctrl.todos"></todo>
</ul>
todo.html
<div>
<p ng-repeat='todo in $ctrl.todos track by $index'>Todo:
{{$ctrl.todo.name}}
</p>
</div>