2

I am having a problem sending a value of JavaScript variable to Angular js variable. I want to send a value in dataArray variable in JavaScript to Angular js variable $scope.test

html code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

     <script src="angular.js"></script>
   <script type='text/javascript'>
 $(document).ready(function () {
   // $("#fileUpload").load('test.csv');
    $.get("test.csv", function(data) {
            alert(data);
            var rows = data.split("\r\n");

                    if(rows.length>0){
                        alert("inside if");
                        var firstRowCells = GetCSVCells(rows[0], ",");

                        var dataArray = new Array();
                        for(var i=1;i<rows.length;i++)
                        {
                            var cells = GetCSVCells(rows[i], ",");
                            var obj = {};
                            for(var j=0;j<cells.length;j++)
                            {
                                obj[firstRowCells[j]] = cells[j];
                            }
                            dataArray.push(obj);
                        }


                        $("#dvCSV").html('');
                        alert(dataArray);
                        $("#dvCSV").append(JSON.stringify(dataArray));
                        var myjson=JSON.stringify(dataArray);
                        //alert(myjson);
                    }

    });
    function GetCSVCells(row, separator){
    return row.split(separator);
}
});


</script>
</head>
<body>
    <div id="container">
        Test
    </div>
<div  ng-app="sortApp" ng-controller="mainController">
    <div id="dvCSV" ng-model="dataf" ng-bind="bdc">dfsgdfd</div>
</div>
<script src="app.js"></script> 
</body>
</html>

app.js:

angular.module('sortApp', [])
.controller('mainController', function($scope) {
  window.alert("Angular");
  window.alert("asdfad"+$scope.bdc);
  $scope.test=$scope.dataf;
  window.alert($scope.myjson);

  window.alert("test"+$scope.test.value);
3
  • You lack basic understanding of both javascript and angular, I suggest do some proper reading Commented Aug 25, 2016 at 10:24
  • don't mix jquery with angular. you should be able to run the code segment in the $get from inside the controller on intialize. Commented Aug 25, 2016 at 10:26
  • can you please tell me how to pass a variable value from javascript to angularjs Commented Aug 25, 2016 at 10:35

2 Answers 2

0

You can do this all jquery stuff in angular using http service of angular js. For simple http service you can refer this link - http://www.w3schools.com/angular/angular_http.asp

Sign up to request clarification or add additional context in comments.

4 Comments

i have to send a value from javascript to angularjs
you can set using this - var myEl = angular.element(document.querySelector('#mainController')); var myScope = angular.element(myEl).scope(); myScope.data = data; After this code your scope data is changed.
i cant understand what you are saying can you brief it
From JS where your data is coming from csv. You can set value in angular js scope variable. using this code - var myEl = angular.element(document.querySelector('#mainController')); var myScope = angular.element(myEl).scope(); myScope.data = data; For more details check this link - link
0

I agree with previous answer. Also its wrong to use $(document).ready along with using angular framework in you application.

Try something like this:

angular.module('sortApp', [])
  .service('loadTestCsv' ['$http', function($http) {
    return $http.get('test.csv').then(data => {

     // do all data processing you need
     return data;
    });
  }]); 
  .controller('mainController', ['$scope', 'loadTestCsv', function($scope, loadTestCsv) {
     loadTestCsv().then(data => {
       $scope.data = data;
     });
  }]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.