简体   繁体   中英

how to use functions from other js libraries in angular factory and controllers?

Is there any way I can use functions of these libraries in my angular factory?

<head>    
<script type="text/javascript" src="js/3rdparty/strophe.js"></script>
<script type="text/javascript" src="js/3rdparty/xml2json.js"></script>
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controller.js"></script>
</head>

I have a factory and a controller. I want to use the libraries (strophe.js, xml2json.js) with my angular code.

This is how I a using it but it is always giving me an error

 angular.module('fairy_chat.services',['globals'])

// ----------- chat factory :  ------------------ 
 .factory('chat_factory',  function (CONSTANT, strophe){
     var chat_service_obj = {
        var connection = new Strophe.Connection(CONSTANT.BOSH_SERVICE);
        console.log(connection);
    connect_server:function (){
            console.log('constant=='+ CONSTANT.BOSH_SERVICE );
    }

     }
     return chat_service_obj;
});

My controller.js

angular.module('fairy_chat.controllers',['fairy_chat.services','ionic','globals'])

.controller('LoginCtrl', function($scope, $state, chat_factory) {

    $scope.data = {
        username:"",
        password:""
    };

    $scope.login = function(strophe){
        chat_factory.connect_server();
    }
})

You've got a var within a var
This doesn't look right:

var chat_service_obj = {
        var connection = new Strophe.Connection

Should be more like

var chat_service_obj = {
        connection: new Strophe.Connection

Try this for your 'chat factory' definition:

// ----------- chat factory :  ------------------ 
.factory('chat_factory', function(CONSTANT, strophe) {

    var connection = new Strophe.Connection(CONSTANT.BOSH_SERVICE);
    console.log(connection);

    var chat_service_obj = {
        connect_server: function() {
            console.log('constant==' + CONSTANT.BOSH_SERVICE);
        }
    }
    return chat
    _service_obj;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM