Câu hỏi phỏng vấn Angular
Câu hỏi

Is there no equivalent to $scope.emi...

Câu trả lời

In Angular, the equivalents to $scope.$emit() and $scope.$broadcast() from AngularJS do not exist in the same form. Angular has moved away from the scope-based event system that was a core part of AngularJS. Instead, Angular uses a component-based architecture where data and events are handled differently.

In AngularJS, $scope.$emit() and $scope.$broadcast() were used for communication between controllers and directives, which could be nested or hierarchically related. $scope.$emit() allowed an event to bubble up from a child scope to its parent scopes, while $scope.$broadcast() sent events down from the parent scope to child scopes[3].

Angular, however, does not use $scope for component interaction. Instead, it employs a hierarchical dependency injection system and uses @Input() and @Output() decorators along with EventEmitter for component interaction. Here’s how Angular handles these scenarios:

  1. @Output and EventEmitter: To emit events from a child component to a parent component, Angular uses the @Output() decorator along with an instance of EventEmitter. This is somewhat analogous to $scope.$emit() in that it allows child components to send data to parent components[1].

  2. Service with Observables: For broader communication needs, such as across non-hierarchically related components or throughout the application, Angular uses services combined with observables from the RxJS library. A service can be injected into any component, and observables can be used to emit data and subscribe to data changes. This method replaces the need ...

expert

expert

Gợi ý câu hỏi phỏng vấn

senior

What are the advantages with AOT?

junior

What's the difference between an Angular Component and Module ?

middle

Do I always need a Routing Module?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào