简体   繁体   English

了解基于组件的架构的AngularJS方法

[英]Understanding the AngularJS Approach to component-based architecture

Recently I dived into a project using AngularJS and found myself in a world of mess. 最近,我进入了一个使用AngularJS的项目,发现自己陷入了一片混乱。 I am having a hard time trying to understand how Angular should be applied to my project. 我很难理解如何将Angular应用于我的项目。

A little background about my project, I have multiple sections that needs to be loaded at the start. 关于我的项目的一些背景知识,我有多个部分需要在开始时加载。 Think of it as a SPA that has a tall content with multiple sections. 可以将其视为具有多个部分内容的SPA。

In these sections, I'm trying to include components/directives. 在这些部分中,我试图包括组件/指令。 They may contain parent-child components/directives or they could be siblings components/directives. 它们可能包含父子组件/指令,也可能是兄弟姐妹组件/指令。

I love the concept of modularising them but I have exactly no idea how I can let them communicate with each other. 我喜欢将它们模块化的概念,但是我完全不知道如何让它们彼此通信。 For parent-child directives/components, I understand I can use includes/requires. 对于父子指令/组件,我知道我可以使用包含/要求。

But if I had a sibling components, eg a preloader and a carousel gallery component, I can't find any way to let them talk to each other. 但是,如果我有一个兄弟组件,例如预加载器和轮播画廊组件,我找不到任何让他们互相交谈的方法。 I suspect that my understanding of the approach and the architecture is totally wrong. 我怀疑我对方法和体系结构的理解是完全错误的。

Please enlighten me and finally put me in the right direction of how to tackle this situation. 请开导我,最后使我朝正确的方向处理该情况。

Thanks guys. 多谢你们。

Since 1.5 there are components . 从1.5开始有组件 As you can read in the docs, they should have only INs (denoted by < binding) and OUTs ( & ). 正如您在文档中可以看到的那样,它们应仅具有IN(由<绑定表示)和OUT( & )。

When you want two sibling components to communicate, you have to do this by some parent component which will assign one component's OUT to another's IN. 当您希望两个同级组件进行通信时,您必须由某个父组件执行此操作,该父组件会将一个组件的OUT分配给另一个组件的IN。 See a simple example: 看一个简单的例子:

 angular.module('gyeong', []) .component('myView', { template: '<component-a on-some-task-finish="$ctrl.resultFromA = result"></component-a>' + '<component-b priceless-result="$ctrl.resultFromA"></component-b>' }) .component('componentA', { bindings: { 'onSomeTaskFinish': '&' }, template: '<p>This is component A. I {{ $ctrl.myData ? "have finished loading" : "am currently loading" }} the data.</p>', controller: function($timeout) { var self = this; $timeout(function() { self.myData = 'This is THE result'; self.onSomeTaskFinish({ result: self.myData }); }, 2000); } }) .component('componentB', { bindings: { 'pricelessResult': '<' }, template: '<p>This is component B. I {{ !$ctrl.pricelessResult ? "am waiting for the result" : "have received the result! It is " + $ctrl.pricelessResult }}.</p>' }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> <div ng-app="gyeong"> <my-view></my-view> </div> 

Of course, there are other options (events or services as you mentioned). 当然,还有其他选择(如您提到的事件或服务)。 But this is a preferred one (IMO) as it maintains single-responsibility components and does not expose their internal implementations (you have noticed I had given different name for the same result in each "scope", havent' you?). 但是,这是首选的(IMO),因为它维护单一职责组件并且不公开其内部实现(您已经注意到我在每个“范围”中为同一结果指定了不同的名称,没有吗?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM