在Ionic / Angular 2中执行成功回调函数后更改按钮颜色

阿姆利特·索哈尔(Amrit Sohal)

首先,我是一个编程新手,我试图构建一个离子应用程序,用于通过蓝牙将命令发送到arduino。

   <ion-content padding>

   <div padding>

      <div ion-button clear block medium>
          <a ion-button color="energetic" outline  full  (click)="tab=1" [outline]="tab==2"> Slider</a>
          <a ion-button color="danger" full   [outline]="tab==1" (click)="tab=2"> Position</a>
      </div>
  </div>
<div [hidden]="tab==2">
  <ion-card>
    <ion-card-header text-center>
      <h2>Turn Light ON/OFF</h2>
    </ion-card-header>
    <ion-card-content>

      <div text-center>
        <button ion-button icon-left color="primary" round (click)="toggle()" large>
                      <ion-icon name="ios-sunny-outline"></ion-icon>
                      ON/OFF
                      <ion-icon name="sunny"></ion-icon>
                    </button>

      </div>


      <ion-list>
        <ion-item>
          <ion-label> OFF/ON </ion-label>
          <ion-toggle checked="false" (ionChange)="toggle()"></ion-toggle>
        </ion-item>
      </ion-list>



    </ion-card-content>
  </ion-card>
</div>
<div [hidden]="tab==1">
<div text-center>

    <button ion-button icon-left color="dark" round outline (click)="Enable()">
  <ion-icon name="bluetooth" color="danger"></ion-icon>
  Show Bluetooth List
</button>
  </div>

  <ion-card>
    <ion-list *ngFor="let list of lists">
     <button ion-item (click)="connect(list.address)">
      <ion-icon name="bluetooth" item-left></ion-icon>
      {{list.name}}
    </button> 
    </ion-list>
  </ion-card>
  <ion-card>
  <ion-card-header text-center color="danger">
    Status
  </ion-card-header>
  <ion-card-content text-center>
    {{status}}
  </ion-card-content>
  </ion-card>

</div>
 </ion-content>

此函数遍历已配对的设备,并且每当我单击特定的按钮/设备时,都会触发connect()函数,并且在连接成功时会调用成功的回调函数。

    @Component({
  selector: 'page-home',
  templateUrl: 'home.html',

})
export class HomePage {

  constructor(public navCtrl: NavController) {

  }


public tog: any = 'OFF';
 public tab= 1;

  toggle() {
    switch (this.tog) {
      case 'ON':
        this.Write('OFF');
        this.tog = 'OFF';

        break;
      case 'OFF':
        this.Write('ON');
        this.tog = 'ON';

      default:
        break;
    }
  }

  status : any;
  lists : any ;
  Enable(){

    BluetoothSerial.isEnabled().then((data)=> {
      this.status = 'Bluetooth is ON';
      BluetoothSerial.list().then((allDevices) => {            
            this.lists = allDevices;
        });
        BluetoothSerial.discoverUnpaired().then((devices)=>{
          this.lists.push(devices);
          this.status= 'discovered some devices';
        }).catch((err)=> { this.status= 'No devices were found'; });
    }).catch((error)=>{ this.status='Bluetooth is not turned on'; });    
  }

  connect(mac){

    BluetoothSerial.connect(mac).subscribe((success)=>{

      this.status= 'Bluetooth connection is successful with'+ success;
    });
  }

 Write (msg){

   BluetoothSerial.write(msg).then( res =>{
    this.status = ' This has been written :' + res;
   }).catch(res=> function(){
     this.status = ' This has not been written :' + res;
   });


 }
}

问题:.subscribe不会更新视图上的this.status变量,除非我切换到其他页面并再次返回到它。

要求:我想通过更改所选按钮的颜色向用户显示已连接到所选设备。另外,当它正在连接到蓝牙设备时,我想显示某种通知它正在连接到蓝牙设备,请稍候?我该如何实施?

我将不胜感激任何帮助!

谢谢

nylates

对于等待通知,您需要实现loadingController(放置在connect()函数中的代码中)。

对于按钮颜色,

我的建议:您应该使用lists数组deviceListWithStatus:Array<any>在该数组中创建另一个数组,然后从中推入每个对象lists {device:[a device object from the list],status:[the assoc status value]}

  status : any;
  lists : any ;
  deviceListWithStatus:Array<any>;
  Enable(){
    deviceListWithStatus = [];

   BluetoothSerial.isEnabled().then((data)=> {
      this.status = 'Bluetooth is ON';
      BluetoothSerial.list().then((allDevices) => {            
            for(let i in allDevice){
       deviceListWithStatus.push({device:allDevice[i],status:'disconnected'})
            }
        });
        BluetoothSerial.discoverUnpaired().then((devices)=>{
            for(let i in devices){      deviceListWithStatus.push({device:devices[i],status:'disconnected'})
            }
          this.status= 'discovered some devices';
        }).catch((err)=> { this.status= 'No devices were found'; });
    }).catch((error)=>{ this.status='Bluetooth is not turned on'; });    
  }

然后在您的html模板中:

    <ion-list *ngFor="let dev of deviceListWithStatus">
     <button ion-item [ngStyle]="(dev.status=='connected')?{'background-color':  'green'}:{'background-color':  'red'}" (click)="connect(dev)">
      <ion-icon name="bluetooth" item-left></ion-icon>
      {{dev.device.name}}
    </button> 
    </ion-list>

然后更改您的connect功能,如下所示:

  connect(dev){
      let loading = this.loadingCtrl.create({
        content: 'Please wait...'
      });

      loading.present();

    BluetoothSerial.connect(dev.device.address).subscribe((success)=>{
      loading.dismiss();
      dev.status = 'connected';
      this.status= 'Bluetooth connection is successful with'+ success.;
    });
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Ionic + Angular不将所选值附加到回调中以形成输入

来自分类Dev

ionic2 + angular2-单击禁用按钮

来自分类Dev

Angular 2(Ionic 2):拦截Ajax请求

来自分类Dev

Angular2 / Ionic2-选中单选按钮后切换内容

来自分类Dev

将JavaScript \ Ionic \ Angular 1应用迁移到Typescript \ Ionic 2 \ Angular 2应用

来自分类Dev

Ionic 2 / Angular 2中的承诺,怎么办?

来自分类Dev

在Ionic 2 / Angular 2 beta 10中访问窗口对象

来自分类Dev

Angular JS-Ionic-从数组中删除

来自分类Dev

模拟$ http Angular Ionic

来自分类Dev

Angular / IONIC模型之和

来自分类Dev

Angular 2(Ionic 2):拦截Ajax请求

来自分类Dev

Angular2 / Ionic2-选中单选按钮后切换内容

来自分类Dev

以Angular 2 / Ionic 2中的形式访问选定的对象属性

来自分类Dev

Ionic 2 / Angular 2中的承诺,怎么办?

来自分类Dev

带有Angular JS 1或Angular JS 2的Ionic 1

来自分类Dev

Angular 2 / Ionic 2中的Typescript转换错误

来自分类Dev

Ionic 2 / Angular 2:如何从Ionic 2警报上的超链接或按钮发出单击事件?

来自分类Dev

Ionic 2/Angular 2 显示阵列

来自分类Dev

Ionic 2 / Angular - 操作 JSON

来自分类Dev

如何更改 Angular 2 / ionic 2 中的特定属性值?

来自分类Dev

Angular2、TypeScript、Ionic 2 代码执行顺序

来自分类Dev

在 Angular/Ionic 2 中获取选定的 ngModel 值?

来自分类Dev

Ionic / Angular:避免 ExpressionChangedAfterItHasBeenCheckedError

来自分类Dev

Ionic 2 / Angular 2 从数组中的对象获取数据

来自分类Dev

Angular 2 和 Ionic 2 与 Visual Studio

来自分类Dev

不寻常的 Typescript 执行顺序 - Angular/Ionic

来自分类Dev

在组件 Ionic 3 / Angular 5 中动态更改 Sass 变量

来自分类Dev

Angular Ionic 风格改变

来自分类Dev

Ionic Angular 中的 HttpErrorResponse

Related 相关文章

  1. 1

    Ionic + Angular不将所选值附加到回调中以形成输入

  2. 2

    ionic2 + angular2-单击禁用按钮

  3. 3

    Angular 2(Ionic 2):拦截Ajax请求

  4. 4

    Angular2 / Ionic2-选中单选按钮后切换内容

  5. 5

    将JavaScript \ Ionic \ Angular 1应用迁移到Typescript \ Ionic 2 \ Angular 2应用

  6. 6

    Ionic 2 / Angular 2中的承诺,怎么办?

  7. 7

    在Ionic 2 / Angular 2 beta 10中访问窗口对象

  8. 8

    Angular JS-Ionic-从数组中删除

  9. 9

    模拟$ http Angular Ionic

  10. 10

    Angular / IONIC模型之和

  11. 11

    Angular 2(Ionic 2):拦截Ajax请求

  12. 12

    Angular2 / Ionic2-选中单选按钮后切换内容

  13. 13

    以Angular 2 / Ionic 2中的形式访问选定的对象属性

  14. 14

    Ionic 2 / Angular 2中的承诺,怎么办?

  15. 15

    带有Angular JS 1或Angular JS 2的Ionic 1

  16. 16

    Angular 2 / Ionic 2中的Typescript转换错误

  17. 17

    Ionic 2 / Angular 2:如何从Ionic 2警报上的超链接或按钮发出单击事件?

  18. 18

    Ionic 2/Angular 2 显示阵列

  19. 19

    Ionic 2 / Angular - 操作 JSON

  20. 20

    如何更改 Angular 2 / ionic 2 中的特定属性值?

  21. 21

    Angular2、TypeScript、Ionic 2 代码执行顺序

  22. 22

    在 Angular/Ionic 2 中获取选定的 ngModel 值?

  23. 23

    Ionic / Angular:避免 ExpressionChangedAfterItHasBeenCheckedError

  24. 24

    Ionic 2 / Angular 2 从数组中的对象获取数据

  25. 25

    Angular 2 和 Ionic 2 与 Visual Studio

  26. 26

    不寻常的 Typescript 执行顺序 - Angular/Ionic

  27. 27

    在组件 Ionic 3 / Angular 5 中动态更改 Sass 变量

  28. 28

    Angular Ionic 风格改变

  29. 29

    Ionic Angular 中的 HttpErrorResponse

热门标签

归档