Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, August 16, 2016

Alternative to $scope in AngularJS 2.0

Alternative to $scope in AngularJS 2.0


I recently learned that in AngularJS 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.

Answer by Pytth for Alternative to $scope in AngularJS 2.0


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

Answer by rob for Alternative to $scope in AngularJS 2.0


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.

Answer by Saidh for Alternative to $scope in AngularJS 2.0


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

Answer by Haringat for Alternative to $scope in AngularJS 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).

Answer by Gnter Zchbauer for Alternative to $scope in AngularJS 2.0


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 = new Subject();  }  
@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.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.