angularjs - how to access angular $scope from a javascript function -


i have added functionality parse csv file in javascript. want assign parsed data $scope.data.arr. currently, below code gives error "uncaught referenceerror: scope not defined". newbie angularjs , have followed official angular tutorials.

the code is:

application.js

'use strict'; /* application module */ var ddvapp = angular.module('ddvapp', ['ddvcontrollers']); 

datacontroller.js

'use strict'; /*data controller*/  var ddvcontrollers = angular.module('ddvcontrollers', []);  ddvcontrollers.controller('datacontroller', ['$scope', function($scope){   $scope.data = {}; //created empty data object. }]); 

read-csv.js

function handlefiles(files) {   // check various file api support.   if (window.filereader) {     // filereader supported.     getastext(files[0]);   }    else {     alert('filereader not supported in browser.');   } }  function getastext(filetoread) {   var reader = new filereader();   // read file memory utf-8         reader.readastext(filetoread);   // handle errors load   reader.onload = loadhandler;   reader.onerror = errorhandler; }  function loadhandler(event) {   var csv = event.target.result;   scope.data.arr = processdata(csv); //this line of code want assign parsed data angularjs $scope. }  function processdata(csv) {   var alltextlines = csv.split(/\r\n|\n/);   var lines = [];   (var = 0; < alltextlines.length; i++) {     var data = alltextlines[i].split(',');     var arr = [];     (var j = 0; j < data.length; j++) {       arr.push(data[j].trim());     }     lines.push(arr);   }   return lines; }  function errorhandler(event) {   if(event.target.error.name == "notreadableerror") {     alert("cannot read file !");   } } 

--update problem statement.

the handlefiles() function called whenever user selects new csv file parsed.

html code:

<input type="file" id="csvfileinput" onchange="handlefiles(this.files)" accept=".csv"> 

i firstly implemented code parse csv in javascript. using data render graph on html canvas. wanted functionality user selects different csv file updated data , graph should update without further inputs. added angularjs because (in future implementations) file , graph need saved db. also, data can requested server instead of user loading using csv file.

while can acccess $scope via angular's element() function looks code better put angular service.

if doing reason not option, need reference dom element belongs controller.

once have that, can do

angular.element(mydomelement).scope() 

and should give reference.

you can use element.find() css selectors aware if have not loaded jquery you're left limited set of selectors (tag-names only).

see docs angular.element


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -