Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to $scope in Angular 2.0

People also ask

Is $scope still supported in angular 2+?

In Angular 2.0, there will be no $scope .

What is difference between $scope and $rootScope object?

The main difference is the availability of the property assigned with the object. A property assigned with $scope cannot be used outside the controller in which it is defined whereas a property assigned with $rootScope can be used anywhere.

What is the difference between $scope and scope in angular?

The $ in "$scope" indicates that the scope value is being injected into the current context. $scope is a service provided by $scopeProvider . You can inject it into controllers, directives or other services using Angular's built-in dependency injector: module.

What is alternative of rootScope in angular?

In similar way, you can use use the sharedService inside any component & fetch the returing output and used inside your view. That's it. I found this solution as a good alternative of rootScope. You can use this inside your angular 2/4 application.


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


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 root component and then the providers registered at bootstrap. It returns the first one it finds.

If two components got the same instance (from the same provider) injected, then they can communicate using this service by subscribing to and emitting events of the Subject or reading and writing the state or by calling methods on the service.


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.


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


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).