Alternative to $scope in Angular 2.0

In Angular 2.0, there will be no $scope.

What is the alternative to that? How will I be able share data between the components? Will the scope option be available in directives? More practically, is there a current alternative to that that I can get acquainted with?

I am aware of controller as but I read that controllers will be eliminated too.

Confused on such a revolution the Angular team has started.

6
Leave a Reply

avatar
6 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Angular 2.0 is using this instead of $scope.

One of the major changes coming in 2.0 is the death of the controller, and a new emphasis on components.
Big advantage of moving towards component-based apps is that it’s easier to define their interfaces; plus, HTML elements already have an easily mappable interface in events, attributes, and properties.

See the Migration of AngularJS 1.3 to 2.0 here.
Also see the complete documentation of Angular 2.0 here

Jason
Guest

In Angular2 you use shared services instead of $scope https://angular.io/docs/ts/latest/cookbook/component-communication.html @Injectable() class SharedService { someState:String; someEventStream:Subject<String> = new Subject<String>(); } @Component({ selector: ..., providers: [SharedService] }) class MyComponent { constructor(private sharedService:SharedService) {} } For each provider ([SharedService] is short for [{provide: SharedService, useClass: SharedService}]) in above example a single instance is maintained. The scope of such a provider is the component where it is provided and it’s descendants when no descendant has the same provider registered. When a component requests a dependency constructor(private sharedService:SharedService), Angulars DI (dependency injection) starts looking at the components own providers and then upwards towards the… Read more »

Jason
Guest

Using controller as is a good way to get used to working without $scope although you’ll still need $scope for a few things like watchers and events. Controllers are not really being removed in Angular 2.0. But there won’t be an equivalent of ng-controller. Instead of having controllers and views you will just have directives which essentially encapsulate the controller and the view.

Jason
Guest

I wouldn’t worry about 2.0 at all. The angular team has said that:

It’s too soon to start building anything with 2.0 code — we’re still in the very early stages of building out the project.

Trying to learn something that is this early in development could largely end up being a huge waste of your time. That being said, if you do want to get a jump start on Angular 2.0, the new router that was introduced with 1.3 is — as of now — the router they intend on using for 2.0

Jason
Guest

Angular 2 does not share data between components like Angular 1 did. Instead what they do is passing down data by using it inside the template and passing up events (by just using the bubble behaviour which events have by default). And you access data from the pattern by using the component class (Have a look at the 1000000000 “angular 2 – How to get started” videos on Youtube if you have no idea what I mean by class).

Jason
Guest

We can do this in angular without injecting an service into component class. By using rxjs subject, ‘Behaviour Subject’, which carries our data fulfilling the same requirement as $scope variable

Here is a stackblitz example where I export an const letter in sub.service.ts which is subscribed in necessary components.