【发布时间】:2019-11-19 12:54:28
【问题描述】:
我希望这样的问题会被回答一百万次,但找不到任何适合我的特定问题的东西。我的组件如下所示:
const todoApp = () => ({
template: `
<div>
<todo-list todos="todoApp._filteredTodos"></todo-list>
</div>
`,
controller: class {
constructor(todoService) {
[...]
}
updateState() {
this._activeTodos = _.filter(this._todos, t => !t.completed);
switch (this.selectedFilter) {
case 'active':
this._filteredTodos = _.filter(this._todos, t => !t.completed);
break;
case 'completed':
this._filteredTodos = _.filter(this._todos, t => t.completed);
break;
default:
this._filteredTodos = this._todos;
}
},
updateTodos() {
this._todos = this.todoService.fetch();
this.updateState();
}
[...]
},
restrict: 'E',
bindToController: true,
controllerAs: 'todoApp',
link: function(scope, elem, attr, ctrl) {
document.addEventListener('store-update', ctrl.updateTodos.bind(ctrl), false);
}
});
export default todoApp;
我需要更新todoApp._todos,以便<todo-list> 使用新的项目集进行更新。这不会在 atm 发生。
<todo-list> 组件非常简单:
const todoList = () => ({
scope: {
todos: '=',
},
template: `
<ul class="todo-list">
<li ng-repeat="todo in todoList.todos track by todo.id">
[...]
</li>
</ul>
`,
controller: class {
[...]
},
restrict: 'E',
bindToController: true,
controllerAs: 'todoList'
});
export default todoList;
我在这里错过了什么?
【问题讨论】:
-
什么触发了
updateState()函数? -
这是一个很好的问题,@georgeawg。我已经用呼叫发起者-
updateTodos ()更新了示例。反过来,updateTodos()是自定义事件的事件处理程序。 -
自定义事件如何与AngularJS框架及其摘要循环集成?只有在 AngularJS 执行上下文中应用的操作才能受益于 AngularJS 数据绑定、异常处理、属性监视等。
-
这是组件的
link@georgeawg 中的简单addEventListener。我已经更新了这个例子。我还能将该事件集成到 AngularJS 执行上下文中吗?
标签: angularjs templates binding