Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何访问JavaScript文件中的角度范围变量_Javascript_Jquery_Angularjs_Highcharts - Fatal编程技术网

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

如何访问JavaScript文件中的角度范围变量,javascript,jquery,angularjs,highcharts,Javascript,Jquery,Angularjs,Highcharts,一般来说,我对angular和javascript都是新手。我知道可能有一个简单的方法可以做到这一点,但我只是有一个困难的时间来弄清楚 我定义了一个角度服务和控制器 var app = angular.module('nodeAnalytics', []); app.factory('myService', function($http) { var myService = { async: function() { // $http returns a promise

一般来说,我对angular和javascript都是新手。我知道可能有一个简单的方法可以做到这一点,但我只是有一个困难的时间来弄清楚

我定义了一个角度服务和控制器

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)
    });
}]);
我知道在我的html文件中,我使用
{{{data}}
来访问
scope.data
$window.data
允许我访问浏览器中的scope元素,但这并没有太大帮助,因为我不知道如何让javascript文件访问窗口元素

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

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

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

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

只要看一下这个简单的代码,你就会明白

<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>

var-app=angular.module('myApp',[]);
应用程序控制器('MainCtrl',函数($scope){
$scope.d=[10,20,30,40];
});
$(函数(){
var scope=angular.element(“#div1”).scope();
$(“#容器”)。高图({
图表:{
类型:“列”
},
标题:{
文本:“带负值的柱状图”
},
学分:{
已启用:false
},
系列:[{
名称:'条形图',
数据:scope.d
}]
});
});
您必须使用
angular.element(#dom.scope()
来访问
$scope.d


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

使用highcharts时,请告诉我一件事?是在窗口对象中设置数据之后还是之前?。在这里,执行顺序很重要。Highcharts是在数据设置后呈现的。您可以熟悉prepared指令,它似乎更易于使用。我不明白如何使用指令将变量传递给给定的模板,你能解释一下吗?你可以将指令绑定到控制器上,然后它就可以正常工作了。“将变量传递到给定模板”是什么意思?这应该可以,但当图形加载时,作用域为空或null。您实际想做什么?您正在谈论的是哪个作用域?我没有理解您??