天天看點

Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論

Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論
Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論
Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論
Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論
Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論
Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論
Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論

/**

    * Invoke this method to explicitly process change detection and its side-effects.

    *

    * In development mode, `tick()` also performs a second change detection cycle to ensure that no

    * further changes are detected. If additional changes are picked up during this second cycle,

    * bindings in the app have side-effects that cannot be resolved in a single change detection

    * pass.

    * In this case, Angular throws an error, since an Angular application can only have one change

    * detection pass during which all change detection must complete.

    * @return {?}

    */

   tick() {

       if (this._runningTick) {

           throw new Error('ApplicationRef.tick is called recursively');

       }

       try {

           this._runningTick = true;

           for (let view of this._views) {

               view.detectChanges();

           }

           if (this._enforceNoNewChanges) {

               for (let view of this._views) {

                   view.checkNoChanges();

               }

       catch (e) {

           // Attention: Don't rethrow as it could cancel subscriptions to Observables!

           this._zone.runOutsideAngular((/**

            * @return {?}

            */

           () => this._exceptionHandler.handleError(e)));

       finally {

           this._runningTick = false;

   }

Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論
Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論

在core.js的rootContext屬性裡,展開Components, 能看到所有AppComponent的屬性和值:

Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論

渲染UI:

Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論
Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論
Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論

下面這是一個非常重要的從Component屬性将值移動到UI上的方法:

Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論

renderer.setProperty:

Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論
Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論

在這裡給input的value屬性賦的值:

Angular應用從Component到Html的資料綁定是如何實作的 -資料流的讨論

繼續閱讀