Two process in a single controller? is it possible?


#1

I have a code for bluetooth and its perfectly working and Another code for localstorage that is working fine too
but how can i make this two block of codes work in one single page? need help badly. I place them in my index.js.


#2

Set up two factories: One for your local storage methods and the other for your bluetooth methods. Use both factories (which are singleton classes) in your controller of your view. That’s all, it’s really easy task.


#3

Oh … by the way … if you want to use two asynchronous processes, you should work with events or promises.

Here’s an example for your own homebrewn promise:

var myMethod = function() {
    var q = $q.defer();
    if (...) q.resolve(yourResult); // Resolves your promise
    else q.reject(yourError); // Rejects your promise
    return q.promise;
}

var anotherMethod = function() {
    myMethod().then(
        function(response) { console.log(response); }, // "response" has the value of "yourResult" above ...
        function(error) { console.log(error); } // "error" has the value of "yourError" above ...
    );
}

Still any questions?