博客 页面 14

angular中事件监听后this传导问题

原本的写法:

window.addEventListener("deviceorientation", this.watch);
window.removeEventListener("deviceorientation", this.watch);

这样在this.watch中无法调用正确的this对象

通过this.watch.bind(this);传递了this后,removeEventListener又失效了。

后查得,可以使用Renderer2提供的listen方法:

import { Renderer2 } from '@angular/core';

  constructor(
    private renderer: Renderer2,
  ) {}

  start() {
    this.listenFunc = this.renderer.listen("window", "deviceorientation", (e) => {
      console.log(e.alpha, e.beta, e.gamma);
    });
  }

  stop() {
    this.listenFunc();
  }

调用返回函数listenFunc,即可取消监听。

cordova/ionic中获取手机AHRS姿态数据

一开始使用原生代码获取,经同事提示,可以使用w3c标准接口获取,更简单。

使用W3C标准的设备方向获取代码:

参考代码:

  watch(event) {
    console.log(event.alpha, event.beta, event.gamma);
  }

  start() {
    window.addEventListener("deviceorientation", this.watch);
  }

  stop() {
    window.removeEventListener("deviceorientation", this.watch);
  }

参考网站:

https://www.w3.org/TR/2016/CR-orientation-event-20160818/

https://www.w3.org/TR/2017/NOTE-orientation-event-20170530/

PS:在最新版文档中提示,这个规范不再更新,可能有一定安全问题。

ionic 按两次返回键退出app

参考:https://segmentfault.com/a/1190000010364414

ionic中使用管道pipe的问题

在ionic中使用pipe提示:

The pipe ‘layoutPipe’ could not be found

app.module.ts里已经import了

 

经过测试发现,app.module.ts里import无效,在要使用的组件components.module.ts中import才行

为什么LED会烧

王工谆谆教诲:

因为LED是负温度系数的 温度越高电流越大
【潜水】翔翔 2018/2/11 17:51:01
电流越大发热越大
【潜水】翔翔 2018/2/11 17:51:12
然后停不住就烧了。。
17:53:10
【潜水】213 2018/2/11 17:53:10
原来如此。。。我就奇怪为啥只多了0.1v就烧了
17:55:13
【潜水】翔翔 2018/2/11 17:55:13
串电阻就限制了这个过程,温度升高,电流增加,电阻上的分压增大,led上的电压减小,电流就减小,温度就不会再上升。

问题记录

使用ionic ble插件遇到的问题,搜索附近ble设备,android上获取到的是mac地址,ios上获取到的是uuid,但这和我需求不符合了,暂时没找到ios获取ble设备mac地址的方法。

vs code调试cordova/ionic程序配置

 

之前用xcode调试ionic程序多有不便,比如console.log输出一个对象,xcode中只能看到输出“object”。

js相关代码的数据,还是要在vscode中看才行。

使用vscode调试ionic项目时,可以安装运行app,但却无法连接到调试模式,vs code提示:

Unable to communicate with target

cordova unable to find webview

解决办法:

设置》safari浏览器》高级》开启 web检查器 选项

ionic使用AlertController报错

提示:

Error: Uncaught (in promise): removeView was not found

Error: Uncaught (in promise): inserted view was already destroyed

 

  showToast(code) {
    let toast = this.toastCtrl.create(this.mess[code]);
    toast.onDidDismiss(() => {
      console.log('Dismissed toast');
    });
    toast.present();
  }

 

 

还没找到原因。。。

ionic手势识别开发

在ionic文档中对于手势的描述只有一句话:
Gestures
Basic gestures can be accessed from HTML by binding to tap, press, pan, swipe, rotate, and pinch events.

几乎等于不写,实际测试swipe也用不了,ionic群里有人说swipe在ionic3中默认禁用了。
尝试使用pan获取拖动数据,但pan只能在x方向滑动才能触发。

另有关于手势的一些介绍:https://www.jianshu.com/p/08fd66a34af8

但基本没啥用。

于是决定研究hammerjs,ionic中实际使用的也是hammerjs
————————————————
2018.1.25
参考:https://www.jianshu.com/p/08fd66a34af8
pan就是横向滑动,所以只能横向滑动触发

改用pinch来缩放组件,但pinch也无法直接使用,查资料后了解其用法如下:

import { Gesture } from ‘ionic-angular’;


private gesture: Gesture;
@ViewChild('image') ElementRef ;

...

ionViewDidLoad() {
//create gesture obj w/ ref to DOM element
this.gesture = new Gesture(this. el.nativeElement);

//listen for the gesture
this.gesture.listen();

//turn on listening for pinch or rotate events
this.gesture.on('pinch', e => this.pinchEvent(e));
}

private pinchEvent(event) {
    console.log(event);
}

事件和参数设置可以参考hammer文档:http://hammerjs.github.io/recognizer-pan/

ionic生效swipe事件

群里的朋友提供的方法,还没验证过

npm install hammerjs –save && npm install @types/hammerjs –save-dev
创建MyHammerConfig.ts
import { HttpClient } from ‘@angular/common/ http’; //空格去掉
import { Injectable } from ‘@angular/core’;
import { HammerGestureConfig, HAMMER_GESTURE_CONFIG } from ‘@angular/platform-browser’;
import * as Hammer from ‘hammerjs’;
@Injectable()
export class MyhammerconfigProvider extends HammerGestureConfig {
overrides = {
‘swipe’: { direction: Hammer.DIRECTION_ALL } }
}

在你的app.module文件引入该文件,并在providers里加入以下代码

import { MyhammerconfigProvider } from ‘../providers/myhammerconfig/myhammerconfig’;
import { HAMMER_GESTURE_CONFIG } from ‘@angular/platform-browser’;
//上下滑动
{ provide: HAMMER_GESTURE_CONFIG, useClass: MyhammerconfigProvider }

html可以通过swipeEvent方法就可以监听到向上和向下的滑动了。