Is there no equivalent to $scope.emi...
Is there no equivalent to $scope.emi...
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:
@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].
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
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào