如何在javascript文件中访问角范围变量

时间:2022-08-27 17:42:39

I'm really new to angular and javascript in general. I know there is probably an easy way to do this but I'm just having a hard time figuring it out.

总的来说,我是一个全新的角度和javascript。我知道可能有一种简单的方法可以做到这一点,但我只是很难弄明白。

I have an angular service and controller defined.

我定义了角服务和控制器。

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

app.factory('myService', function($http) {
  var myService = {
    async: function() {
      // $http returns a promise, which has a then function, which also returns a promise
      var promise = $http.get('https://graph.facebook.com/lowes').then(function (response) {
        // The then function here is an opportunity to modify the response
        console.log(response);
        // The return value gets picked up by the then in the controller.
        return response.data;
      });
      // Return the promise to the controller
      return promise;
    }
  };
  return myService;
});


app.controller('MainCtrl', [
'$scope', 'myService', '$window',
  function($scope, myService, $window){
    $scope.test = 'Hello world!';
     myService.async().then(function(d) {
      $scope.data = d.likes;
      $window.data = $scope.data;
      console.log($scope.data)
    });
}]);

I know that in my html file I an use {{data}} to access scope.data. $window.data allows me to access the scope element in the browser but that has not been very helpful since I don't know how to give the javascript file access to window elements.

我知道在我的html文件中,我使用{{data}}来访问scope.data。美元的窗口。数据允许我访问浏览器中的scope元素,但这并没有多大帮助,因为我不知道如何为窗口元素提供javascript文件访问。

How do I access the data variable in a javascript/jquery file.

如何访问javascript/jquery文件中的数据变量。

I'm using highcharts and I want to put the value of data into a chart argument but I don't know how to access it.

我使用的是highcharts,我想把数据的值放到一个图表参数中,但是我不知道如何访问它。

  $('#container-rpm').highcharts(Highcharts.merge(gaugeOptions, {
            series: [{
                data: {{data}},
            }]
        }));

2 个解决方案

#1


2  

Just have a look at this simple code you will understand

看看这个简单的代码就会明白

<body ng-app="myApp" data-ng-controller="MainCtrl" id="div1">
  <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto">
  </div>
 <script>
  var app = angular.module('myApp', []);

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

    $scope.d=[10, 20, 30, 40];

    });
  </script>
  <script>
$(function () {

  var scope = angular.element("#div1").scope();
    $('#container').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Column chart with negative values'
        },
        credits: {
            enabled: false
        },
        series: [{
        name: 'Bar Chart',
        data: scope.d

        }]
    });
});
</script>

</body>

You have to use angular.element(#dom).scope() to access $scope.d

必须使用angular.element(#dom).scope()访问$scope.d

if you want to make changes in the value of array d before plotting graph you have to use $scope.$apply().

如果要在绘制图形之前修改数组d的值,必须使用$scope.$apply()。

#2


0  

you need to place you chart in a directive. From the directive, you ll then be able to access the scope.

你需要把你的图表放在一个指示。从指令中,您将能够访问范围。

#1


2  

Just have a look at this simple code you will understand

看看这个简单的代码就会明白

<body ng-app="myApp" data-ng-controller="MainCtrl" id="div1">
  <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto">
  </div>
 <script>
  var app = angular.module('myApp', []);

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

    $scope.d=[10, 20, 30, 40];

    });
  </script>
  <script>
$(function () {

  var scope = angular.element("#div1").scope();
    $('#container').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Column chart with negative values'
        },
        credits: {
            enabled: false
        },
        series: [{
        name: 'Bar Chart',
        data: scope.d

        }]
    });
});
</script>

</body>

You have to use angular.element(#dom).scope() to access $scope.d

必须使用angular.element(#dom).scope()访问$scope.d

if you want to make changes in the value of array d before plotting graph you have to use $scope.$apply().

如果要在绘制图形之前修改数组d的值,必须使用$scope.$apply()。

#2


0  

you need to place you chart in a directive. From the directive, you ll then be able to access the scope.

你需要把你的图表放在一个指示。从指令中,您将能够访问范围。