[英]How to pass data between two components in angularjs syntax es6?
I would like to pass data from the todos component to the todo component and display all elements in the form of a list. 我想将数据从todos组件传递到todo组件,并以列表的形式显示所有元素。 I download data from the service and place it in the
todos.controller
. 我从该服务下载数据并将其放置在
todos.controller
。 In todos.component
, I use bindings: {todos: '<'}
. 在
todos.component
,我使用bindings: {todos: '<'}
。 In the todos.html
view, it provides data in the form of todos = $ ctrl.todos
. 在
todos.html
视图中,它以todos = $ ctrl.todos
的形式提供数据。 In todo.html
, it iterates over todos
and wants to return todo.name
. 在
todo.html
,它遍历todos
并想返回todo.name
。 Effect: returns only 'Todo'
. 效果:仅返回
'Todo'
。
todo.service.js todo.service.js
export class TodoService {
constructor($http) {
'ngInject';
this.$http = $http;
}
getTodos() {
return this.$http.get('/api/todos');
}
}
todos.controller.js 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 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 todos.html
<h1>Todos</h1>
<ul>
<todo todos="$ctrl.todos"></todo>
</ul>
todo.html todo.html
<div>
<p ng-repeat='todo in $ctrl.todos track by $index'>Todo:
{{$ctrl.todo.name}}
</p>
</div>
Change todo.html
as follows: 如下更改
todo.html
:
<div>
<p ng-repeat='todo in $ctrl.todos'>Todo:
̶{̶{̶$̶c̶t̶r̶l̶.̶t̶o̶d̶o̶.̶n̶a̶m̶e̶}̶}̶
{{todo.name}}
</p>
</div>
Also with todos.html
: 也可以使用
todos.html
:
<h1>Todos</h1>
̶<̶u̶l̶>̶
<todo todos="$ctrl.todos"></todo>
̶<̶/̶u̶l̶>̶
The only permitted content of <ul>
elements is zero or more <li>
elements. <ul>
元素的唯一允许内容是零个或多个<li>
元素。
For more information, see 有关更多信息,请参见
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.