无法在Angular工厂调用服务方法(使用Ionic)

时间:2016-11-09 08:54:54

标签: angularjs ionic-framework

使用Ionic v1并尝试使用thisself从工厂内调用函数,但收到错误消息:

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;

    });

1 个答案:

答案 0 :(得分:1)

您必须致电hardwareService.connectArd(MACdevicex)

由于您要为hardwareService对象分配函数,因此必须从那里调用它。而不是从工厂范围

调用它