JavaScript Integrating external JS into angular controller

I have modified a simple nfc on-detect, to send a simple byte array over in an separate cordova app using chariotstudios-nfc-plugin . The code is shown below.

index.js

 var app = {
 // Application Constructor
 initialize: function() {
 this.bindEvents();
 console.log("Starting NFC Reader app");
 },
 // Bind Event Listeners
 bindEvents: function() {
 document.addEventListener('deviceready', this.onDeviceReady, false);
 },
 // deviceready Event Handler 
 onDeviceReady: function() {
 //app.receivedEvent('deviceready');
 nfc.addTagDiscoveredListener(
 app.onNfc, // tag successfully scanned
 function (status) { // listener successfully initialized
 app.display("Tap a tag to read its id number.");
 },
 function (error) { // listener fails to initialize
 app.display("NFC reader failed to initialize " +
 JSON.stringify(error));
 }
 );
 },

 onNfc: function(nfcEvent) {
 app.display("invoked function");
 var record = ndef.mimeMediaRecord('text/plain', '888');
 nfc.write(
        [record], 
        function () {
            app.display("Wrote data to tag.");
        }, 
        function (reason) {
          app.display("Problem");
        }
  );   



 },

 display: function(message) {
 var label = document.createTextNode(message),
 lineBreak = document.createElement("br");
 messageDiv.appendChild(lineBreak); // add a line break
 messageDiv.appendChild(label); // add the text
 },


 clear: function() {
 messageDiv.innerHTML = "";
 },


};

index.html

 <script type="text/javascript">
 app.initialize();
 </script>

The above works and tested. The problem I'm facing right now is that I would wish to just implement the nfc.write function into my ionic controller.

Snippet of the controller I'm trying to put (its mostly irrelevant to the question(retrieve items from db))

 .controller('NFC', function($scope, $timeout,$http, $filter,$ionicPopup,       
$stateParams, $cordovaSQLite, $cordovaDatePicker, dataFactory, StoreService){

    dataFactory.getDetails($stateParams.testId, testUserId).then(function(resp) {
    .....

Steps Taken To Resolve:

  1. Calling external JS in the template html (console.log does not fire at all)
  2. Calling JS inside the template html(console.log does not fire at all)

I would like to put this nfc.write into the controller. Any guidance would be deeply appreciated.

Answer:1

You can call any available functionality in your angular controller. Is your functionality global? Thne you can inject $window which is angular's wrapper for global window object and there access the functionality. The other part of story is to make angular process returned value cuz external calls are outside angulars digest cycle. for that check $apply or $digest.

Answer:2

I have an array arr = [ [1,2,3], [4,5,6], [7,8,9] ] I want to remove the array that contains the element 4. Here it would remove 4,5,6. So it'd be arr = [[1,2,3],[7,8,9]]; I tried a for loop but I ...

I have an array arr = [ [1,2,3], [4,5,6], [7,8,9] ] I want to remove the array that contains the element 4. Here it would remove 4,5,6. So it'd be arr = [[1,2,3],[7,8,9]]; I tried a for loop but I ...

  1. remove certain values from list python
  2. remove certain values in r
  3. remove certain values from a list in r
  4. remove certain values from array matlab
  5. remove certain values from vector r
  6. remove certain values from dictionary python
  7. remove certain values from dataframe
  8. remove certain values from array python
  9. remove certain values from list excel
  10. remove certain values from array js
  11. remove certain values from array vba
  12. remove certain values from data frame r
  13. remove certain values from vector matlab
  14. remove certain values from column in r
  15. remove certain values from excel
  16. remove certain values from array php
  17. remove certain values from numpy array
  18. remove certain values pandas
  19. remove certain values from pivot table
  20. remove certain values from excel column

I've got a small problem which has to do with the following approach: I want to iterate through a given array of URLs with a loop I do this here because I want to measure the loading time of each ...

I've got a small problem which has to do with the following approach: I want to iterate through a given array of URLs with a loop I do this here because I want to measure the loading time of each ...

  1. casperjs page.resource.received

I want to toggle the image using if-else but it didn't work, can someone please explain what is wrong with the code. <img src="img/somepic.jpg" id="pic"><br/> <a href="javascript:...

I want to toggle the image using if-else but it didn't work, can someone please explain what is wrong with the code. <img src="img/somepic.jpg" id="pic"><br/> <a href="javascript:...

I'm a beginner in angular test with jasmine and i need to test data type(e.g. String, int, etc.) This is my controller, my data is instantiated to null and later it will be a string : _this....

I'm a beginner in angular test with jasmine and i need to test data type(e.g. String, int, etc.) This is my controller, my data is instantiated to null and later it will be a string : _this....