2017-06-16 7 views

答えて

9

https://angular.io/api/core/ApplicationRef

  • attachView()を使用して含まれているか、または変化検出から除外されるビューを追加/削除することができappRef.tick()
  • を呼び出すことにより、アプリケーション全体の変化の検出を起動することを可能にし、detachView()
  • 提供componentTypesおよびcomponents およびその他の変更検出関連情報を使用する登録済みコンポーネントおよびコンポーネントタイプのリスト
+0

'this'で' app'にアクセスする場合は、 'private'をコンストラクタパラメータに追加する必要があります。 https://plnkr.co/edit/LkYwzZEadoQh4velyEEI?p=preview。私はなぜ 'components'配列が空であるのか分かりません。 –

3

ApplicationRefは、ルート・ビューへの参照が含まれており、手動で明示的変化の検出とその 副作用を処理するためにこのメソッドを呼び出しtick機能

を使用して変化検出を実行するために使用することができます。

開発モードでは、tick()はさらに変更が検出されないように、第2の変更検出 サイクルを実行します。この2番目のサイクルで追加の の変更が取得された場合、 のアプリケーションのバインディングには、単一の変更検出 の合格で解決できない副作用があります。この場合、角度 アプリケーションでは変更検出パスが1つしかないため、すべての変更検出が完了する必要があるため、Angularはエラーをスローします。

ここでは一例であり:

@Component() 
class C { 
    property = 3; 
    constructor(app: ApplicationRef, zone: NgZone) { 
     // this emulates any third party code that runs outside Angular zone 
     zone.runOutsideAngular(()=>{ 
      setTimeout(()=>{ 
      // this won't be reflected in the component view 
      this.property = 5; 

      // but if you run detection manually you will see the changes 
      app.tick(); 
      }) 
     }) 

別のアプリケーションは、それがルートノードを用いて作成された場合、変化検出のための動的に作成されたコンポーネントビューを取り付けることである。

ため
addDynamicComponent() { 
    let factory = this.resolver.resolveComponentFactory(SimpleComponent); 

    let newNode = document.createElement('div'); 
    newNode.id = 'placeholder'; 
    document.getElementById('container').appendChild(newNode); 

    const ref = factory.create(this.injector, [], newNode); 
    this.app.attachView(ref.hostView); 
    } 

チェックthis answer詳細はこちら

関連する問題