title: How to $apply and $digest in Angular 4 and IONIC 3?
body: <p>I am trying to create an application in IONIC 3, but it does not seem to refresh the view properly.</p>
  
  <p>I remember $apply and $digest would solve such kind of issues in Angular 1X.</p>
  
  <p>How is that done in Angular 4?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import { Component } from '@angular/core';
  import { NavController } from 'ionic-angular';
  import { BLE } from '@ionic-native/ble';
  
  @Component({
    selector: 'page-led',
    templateUrl: 'led.html'
  })
  export class LEDPage {
  
    constructor(public navCtrl: NavController, private ble: BLE) {
      this['myCount'] = 0;
      this['valor'] = 0;
      this['messages'] = [];
      this['service_id'] = "19B10010-E8F2-537E-4F6C-D104768A1214";
      this.setMessage("Constructor: Begin!");
      this['ble'] = ble;
      this['ComandoTXT'] = "FIND BLYNK";
      this['targetDevice'] = {};
    }
    
    public Command(){
      if(this['ComandoTXT']==="FIND BLYNK") {
        this.findBlynk();
      }
      
      if(this['ComandoTXT']=== "CAMBIAR LED"){
        //this.txData();
        this.setMessage("CAMBIAR LED txData DISABLEd");
      }
    }
    
    public findBlynk(){
        this.setMessage("SCAN: Begin!");
        this['ComandoTXT'] = "Scanning...";
  
        let  ble = this['ble'];
        
        this.setMessage("SCAN: BLE ENABLE!");
        ble.enable();
        
        this.setMessage("SCAN: Setting Interval...");
        this['intervalHandle'] = setInterval(() =&gt; { // SET AN INTERVAL THAT RUNS EVERY 3 Seconds
          this.setMessage("INTERVAL: BLE SCAN...");
          //https://ionicframework.com/docs/native/ble/ 
          ble.scan([], 2 /*seconds (0) */).subscribe( data =&gt; { //DO SCAN DURING 1 SECOND
            this.setMessage("SCAN SUBSCRIBER: " + data['id'] + ' | ' + data['name'] + ' | ' + data['rssi']);
            if(data['name']=="Blynk"){
              this.setMessage("SCAN SUBSCRIBER: BLYNK FOUND! STOPPED SCANNING!");
              clearInterval(this["intervalHandle"]);
              this["targetDevice"] = data;
              this["ComandoTXT"] = "CAMBIAR LED";
  
              let idx = 0;
              while(idx&lt;10000){
                this["test"] = idx;
                idx ++;
              }
            }
          });
        },2100);//END OF INTERVAL DEFINITION
      }  
    
    /*
    public findBlynk(){
      this.setMessage("findBlynk: BEGIN...");
      this['ComandoTXT'] = "Buscando...";
      this.setMessage("findBlynk: BLE ENABLE...");
      this['ble'].enable();
      this.setMessage("findBlynk: encontrar()...");
  
      this.ble.scan([],500).subscribe(deviceData=&gt;{
        this.setMessage("SUBSCRIBER: Found Device...");
        let deviceMessage = deviceData.id + ":" + deviceData.name + "(" + deviceData.rssi + ")";
        this.setMessage(deviceMessage);
        
        if(deviceData.name == "Blynk"){
          this.setMessage("BLYNK FOUND!");
          this["ComandoTXT"] = "CAMBIAR LED";
          this['dispositivo'] = deviceData;  
          
          this.ble.stopScan().then(() =&gt; {
            this.setMessage('SCANNING STOPPED!');
          });
          
        } else {
          //setTimeout(()=&gt;{this.findBlynk();},500);
        }
      
      }, error=&gt;{
        this.setMessage("BLE SCAN ERROR");
        this.setMessage(error.message);
        setTimeout(()=&gt;{this.findBlynk();},1000);
      });
    }
    
    public txData(){
      this.setMessage("txData: BEGIN! Doing ble connect...");
      let id = this['deviceData'].id;
      this.ble.connect(id).subscribe(datos=&gt;{
        this.setMessage("BLE CONNECT SUBSCRIBE: BEGIN. Doing ble write...");
        this.ble.write(id,this['service_id'],this['service_id'],this['valor'].buffer).then(()=&gt;{
          this.setMessage("BLE WRITE THEN!");
          this['valor'] = (this['valor'] + 1) % 2;
          this.ble.disconnect(id);
        },(error)=&gt;{
          this.setMessage("BLE Write ERROR!");
        });
      },error=&gt;{
        this.setMessage("BLE Connect ERROR!");
      });
    }
    */
  /*
    
    public conectar(id){
      this.ble.connect(id).subscribe(datos=&gt;{
        this.escribir(id);
      },error=&gt;{
        this.encontrar();  
      });
    }
  
    public escribir(id){
  
    }
    
  */
    public setMessage(message){
      this['myCount'] ++;
      message = this['myCount'] + ':' + message;
      this['messages'].unshift(message);
    }
  
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;ion-header&gt;
    &lt;ion-navbar&gt;
      &lt;ion-title&gt;LED &lt;/ion-title&gt;
      &lt;button ion-button (click)="Command()"&gt;{{ComandoTXT}}&lt;/button&gt;
    &lt;/ion-navbar&gt;
  &lt;/ion-header&gt;
  &lt;ion-content padding&gt;
    &lt;p&gt;LED  Messages: ({{myCount}} | {{test}})&lt;/p&gt;
    &lt;hr /&gt;
    &lt;p *ngFor="let message of messages"&gt;{{message}}&lt;/p&gt;
  &lt;/ion-content&gt;</code></pre>
  </div>
  </div>
  </p>