2016-11-20 72 views
0

我有两个组件有很多交叉引用。我决定让他们互相依赖:AngularJS:组件中的相互依赖关系

taskList组件:

angular.module("todo-feature") 
    .component("taskList", { 
     templateUrl: "feature/todo-feature/todo_table.html", 
     controller: "Todo", 
     require: { 
      "parent": "paging" 
     } 
    }); 

paging组件

angular.module("paging-module") 
    .component('paging', { 
     templateUrl: "feature/todo-feature/paging/paging.html", 
     controller: "PagingController", 
     require: { 
      "parent": "taskList" 
     } 
    }); 

index.html

<paging></paging> 
<task-list></task-list> 

它不工作。我收到两个错误:

Controller 'paging', required by directive 'taskList', can't be found! 
Controller 'taskList', required by directive 'paging', can't be found! 

我怎样才能解决这个问题?完全是好建筑吗?你会推荐什么?

+0

无论两个组成部分没有另外一个作为父母。他们是兄弟姐妹。 – estus

回答

1

从设计的角度来看,创建这样一个紧密依赖的组件是不可取的。相反,您可以创建通用服务并在它们之间共享数据,也可以在分页和任务列表组件的顶部创建父组件来管理这两个组件。检查哪一种更适合您的情况。

1

首先需要的语法应该是类似parentCtrl: "^taskList"。这将从其元素到其父母寻找所需的控制器。在api文档here中阅读更多信息。

执行要求在这两个组件引用对方是不可能的,可能它不会工作。

我建议你可以有一个共同的服务或具有父子成分并实现单向绑定