Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 角度2-将一个组件传递到另一个组件_Jquery_Angular - Fatal编程技术网

Jquery 角度2-将一个组件传递到另一个组件

Jquery 角度2-将一个组件传递到另一个组件,jquery,angular,Jquery,Angular,我目前有以下组件树 app.component |-> top-bar.component |-> menu-bar.component |-> bottom-bar.component |-> content-area.component 如果内容区域组件溢出并显示滚动条,我的底部栏组件将更改其行为。我目前使用底部栏中的jQuery查找内容区域组件,并检查滚动条是否显示。然而,为了避免这种紧密耦合,我更愿意将内容区域组件(或其接口)注入底部栏组件,这样我就不必

我目前有以下组件树

app.component
 |-> top-bar.component
 |-> menu-bar.component
 |-> bottom-bar.component
 |-> content-area.component
如果内容区域组件溢出并显示滚动条,我的底部栏组件将更改其行为。我目前使用底部栏中的jQuery查找内容区域组件,并检查滚动条是否显示。然而,为了避免这种紧密耦合,我更愿意将内容区域组件(或其接口)注入底部栏组件,这样我就不必依赖具有CSS类的特定DIV

实现这一目标的最佳方式是什么

[编辑]

我已经尝试了下面的例子,这是我想要实现的事情(contentarea已经在我的@Component输入数组中了)


您将无法将内容组件注入底部,因为它们没有父/子关系

为了能够交换行为、属性和事件,您需要利用共享服务。这样,两个组件将能够一起通信

启动应用程序时需要配置共享服务。这样,同一实例将在整个应用程序中使用

bootstrap(AppComponent, [ SharedService ]);
例如,服务可以处理状态属性:

export class SharedService {
  state:string;
  stateObservable:Observable<string>;

  constructor() {
    this.stateObservable = Observable.create(observer => this.stateObserver = observer).share();
   }

   updateState(newState) {
     this.state = newState;
     this.stateObserver.next(newState);
   }
 }
以及第二个通知此更新的人:

export class BottomComponent {
  constructor(private service:SharedService)
    this.service.stateObservable.subscribe(state => {
      (...)
    });
}

您将无法将内容组件注入底部,因为它们没有父/子关系

为了能够交换行为、属性和事件,您需要利用共享服务。这样,两个组件将能够一起通信

启动应用程序时需要配置共享服务。这样,同一实例将在整个应用程序中使用

bootstrap(AppComponent, [ SharedService ]);
例如,服务可以处理状态属性:

export class SharedService {
  state:string;
  stateObservable:Observable<string>;

  constructor() {
    this.stateObservable = Observable.create(observer => this.stateObserver = observer).share();
   }

   updateState(newState) {
     this.state = newState;
     this.stateObserver.next(newState);
   }
 }
以及第二个通知此更新的人:

export class BottomComponent {
  constructor(private service:SharedService)
    this.service.stateObservable.subscribe(state => {
      (...)
    });
}

对于您的案例,您应该使用a。对于您的案例,您应该使用a