Alternative to $scope in Angular 2.0

后端 未结 6 689
醉话见心
醉话见心 2020-12-04 23:56

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

相关标签:
6条回答
  • 2020-12-05 00:05

    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.

    0 讨论(0)
  • 2020-12-05 00:14

    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.

    0 讨论(0)
  • 2020-12-05 00:14

    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

    0 讨论(0)
  • 2020-12-05 00:14

    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.

    0 讨论(0)
  • 2020-12-05 00:26

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

    0 讨论(0)
  • 2020-12-05 00:28

    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

    0 讨论(0)
提交回复
热议问题