DilumN DilumN - 2 months ago 12
AngularJS Question

Scope of Angular 2 app

In Angular 1, there was a

scope
for every component & we can access them from the html template as well.

So in Angular 2 we can declare components like this,

(function(app) {
app.AppComponent =
ng.core.Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
.Class({
constructor: function() {}
});
})(window.app || (window.app = {}));


My question is, what is the similar thing for
scope
in Angular 2, where we can access from this
template
?

If there is no
scope
in Angular 2, what is the alternative to have a binding of
html
&
javascript
?

Answer

Angular 2 application is building up from isolated from each other components. You can think about that like directives with isolated scope in AngularJS. So you should always pass to component value if you need it from outside.

There are no scopes in Angular2 it has something different called zones. But as an answer to your question: You can just assign the value to this object inside your Class, and it will be available to show in the template.

(function(app) {
  app.AppComponent =
   ng.core.Component({
     selector: 'my-app',
     template: '<h1>{{value}}</h1>'
   })
   .Class({
      constructor: function() { 
        this.value = "myValue";
      }
   });
})(window.app || (window.app = {})); 

More about zones - Zones in Angular2