2016-09-21 4 views
0

각도 1에서는 &의 모든 구성 요소에 대해 scope이 있었으며 html 템플릿에서도이 값에 액세스 할 수 있습니다. 각도 2 앱의 범위

그래서 각도이 우리가이 같은 구성 요소를 선언 할 수

,

(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 = {})); 

내 질문은 우리가이 template에서 액세스 할 수있는, 각도 2 scope에 대한 비슷한 일이 무엇입니까?

각도 2에는 scope가없는 경우 html & javascript의 결합을 가지고있는 대안은 무엇인가?

답변

2

각도 2 응용 프로그램은 서로 다른 구성 요소로부터 격리됩니다. AngularJS에서 격리 된 범위의 지시문과 같은 것을 생각할 수 있습니다. 따라서 외부에서 필요할 경우 항상 구성 요소 값을 전달해야합니다.

Angular2에는 영역이 없으며 영역이라고하는 다른 것이 있습니다. 하지만 귀하의 질문에 대한 대답 : 당신은 단지 클래스 내에 this 개체에 값을 할당 할 수 있으며 템플릿에 표시 할 수 있습니다.

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

영역에 대한 더 - Zones in Angular2

0

각-2에서 당신은 각-1

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