displaying battery status in ionic 2 view

霸气de小男生 提交于 2019-12-11 06:07:54

问题


I am trying to display battery status in my ionic view. I got display network connection solution. but could not get to display battery status, I have installed ionic 2 battery status plugin

HTML

<h2>Battery status: {{status?.level}}</h2>
<h2>Battery is plugged:{{status?.isPlugged}}</h2>

TypeScript

status:any;
constructor(public alert:AlertController, public platform: Platform) { 
  this.platform.ready().then(()=>{
    let subscription = BatteryStatus.onChange().subscribe( (status) => { 
      console.log(status.level, status.isPlugged); 
      this.status=status.level;
    });
  });
} 

You can clone from the below bitbucket git

$ git clone https://bitbucket.org/maniselvam/ionic2battery.git 

回答1:


Import the Battery Status on your TS file.

import { BatteryStatus } from 'ionic-native';

Declare a variable for battery status

Level:any = 0;

Initialise the plugin

let subscription = BatteryStatus.onChange().subscribe(
  (status) => {
    this.Level = status.level;
    console.log(status.level, status.isPlugged);
  }
);

HTML

<h1>Battery Level: {{Level}}</h1>


来源:https://stackoverflow.com/questions/42383317/displaying-battery-status-in-ionic-2-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!