angularjs - JavaScript access to Angular scope returning "undefined" - Asynchronous issue? -
problem: i'm trying data server populate cytoscape graph. when access scope javascript undefined.
despite being quite few posts / similar issue. i'm having trouble finding explanation how round problem of accessing scope javascript file, once data ready (if problem im having).
i read http.get methods asynchronous? don't block after executing. guess causing undefined error when i'm accessing scope because data hasn't returned yet? i've found info on can't figure out use round problem. can access data in usual way in html angular curly braces , works. works when return data controller.
if can point me in right direction i'd appreciate it.
this javascript file im trying run grabs scope , adds nodes cytoscape graph.
<script type="text/javascript"> window.onload = function() { nodes = angular.element(document.queryselector('[ng-controller="datacontroller"]')).scope().nodes; alert(nodes); cy.add(nodes); } </script>
2. calls factory method controller.
datafactory.getnodes().then(function(data) { $scope.nodes = data; });
3. http.get factory
_service.getnodes = function() { return $http.get(urlbase + "/nodes"); };
4. node.js returns 'nodes' add cytoscape graph
router.get('/api/data/nodes', function(req, res) { console.log('sending data'); var data = [ ... ]; res.status(200).json(data); });
i've seen "promises" mentioned guess i'll heading in direction next...
thanks
you trying alert value of nodes when dom loaded (window.onload) , data service not yet returned. controller uses promise, , data binded scope when promise resolved.
.then(function(data){ ... });
if want access data external js script, can call function controller:
script
<script type="text/javascript"> function alertnodes() { nodes = angular.element(document.queryselector('[ng-controller="datacontroller"]')).scope().nodes; alert(nodes); cy.add(nodes); } </script>
controller
datafactory.getnodes().then(function(data){ //$scope.nodes = data; alertnodes() });
Comments
Post a Comment