将javascript变量的值发送到角度js变量

时间:2016-08-25 11:58:27

标签: javascript angularjs

我正在读取一个csv文件并将其转换为数组作为angularjs脚本但我必须将其传递给角度js中的变量 这是一个javascript:

    $.get("test.csv", function(data) {

        var rows = data.split("\r\n");

                if(rows.length>0){
                    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"+dataArray);
                    $("#dvCSV").append(JSON.stringify(dataArray));
                    var myjson=JSON.stringify(dataArray);
                    //alert(myjson);
                }
hera atlast value of dataArray variable have to pass to angular js variable

我必须将它存储在角度js变量$ scope.test中 app.js:

angular.module('sortApp', [])
.controller('mainController', function($scope) {
  $scope.test=$scope.dataf;

2 个答案:

答案 0 :(得分:1)

为什么还需要使用jQuery.get()?使用angular $http

angular.module('sortApp', [])
.controller('mainController', function($scope, $http) {
    $http.get("test.csv")
    .then(function(data) {
        var rows = data.split("\r\n");
        if(rows.length > 0){
            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);
            }
            /*
                You shouldn't use jQuery here anyway,
                just bind a scope value to your html...
                $("#dvCSV").html('');
                alert("dataarray"+dataArray);
                $("#dvCSV").append(JSON.stringify(dataArray));
                var myjson=JSON.stringify(dataArray);
            */
            $scope.test = dataArray;
        }
    })
})

并在您的HTML中:

<div ng-app="sortApp" ng-controller="mainController">
    {{test}}
</div>

答案 1 :(得分:0)

如果你不能使用$ http而不是你可以使用$ apply()来实现这个,请查看片段以了解apply的基本用法。

function jsfun()
{
  var data = "i am Ouside scope var ";
  var elem = document.getElementById("mainDiv");
  var scope = angular.element(elem).scope();
  
  
  scope.$apply(function(){
  
    scope.data = data;
  })
}

var app = angular.module('myapp',[]);

app.controller('demoCtrl',function($scope){

  $scope.data = "";
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myapp'>
  <div id="mainDiv" ng-controller="demoCtrl">
  
    value from js :: {{data}}
    
    <br>
    <button onclick="jsfun()">get data from js</button>
  </div>  
</div>