如何访问 JavaScript 文件中的角度范围变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30688825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:32:06  来源:igfitidea点击:

How to access angular scope variable in JavaScript file

javascriptjqueryangularjshighcharts

提问by Keon

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.

总的来说,我对 angular 和 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.dataallows 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. $window.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}},
            }]
        }));

回答by shreyansh

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 dbefore plotting graph you have to use $scope.$apply().

如果要array d在绘制图形之前更改 的值,则必须使用$scope.$apply().

回答by Ruben

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

您需要将图表放在指令中。从指令中,您将能够访问范围。