使用Ionic v1并尝试使用this
或self
从工厂内调用函数,但收到错误消息:
Uncaught TypeError: Object #<Object> has no method 'connectArd'
代码:
angular.module('hardware.services', [])
.factory('hardwareserv', function($http,$rootScope,$state,$cordovaBluetoothSerial) {
var hardwareService = {};
hardwareService.connectArd = function(MACaddress) {
window.bluetoothSerial.connect(MACaddress, this.successConnect2, this.failConnect);
};
this.connectArd(MACdevicex); // calling above method
return hardwareService;
});
答案 0 :(得分:1)
您必须致电hardwareService.connectArd(MACdevicex)
。
由于您要为hardwareService对象分配函数,因此必须从那里调用它。而不是从工厂范围
调用它