简体   繁体   English

Angular JS指令范围值绑定与控制器未定义

[英]Angular js directive scope value binding with controller undefined

I've created a simple directive that displays a chart. 我创建了一个显示图表的简单指令。

I have an angular directive with an $http request. 我有一个$ http请求的角度指令。 I have to store the response into my $scope and access this $scope value to my link directive scope to display the chart. 我必须将响应存储到$ scope中,并将此$ scope值访问我的link指令范围以显示图表。

I'm trying to implement a gauge chart using am chart angular chart 我正在尝试使用上午图表角度图来实现量表

Here is my code : 这是我的代码:

app.directive('gauge',function(){
    return {
        restrict: 'AEC',
        replace:true,
        template: '<div id="speeda_meter"></div>',
        scope: {ranges:'='},
        controller: function ($scope, $http,apiurl) {
            $scope.type = 'percentage';
            function getUsage(type){ 
                $http({
                    method: 'POST',
                    url: apiurl + '/getUsage',
                    data: {'type': $scope.type}
                }).success(function (data, status) {
                    if (data.status == true) {
                        $scope.ranges = data.result.ranges;
                        $scope.interval = data.result.interval;
                    }    
                });
            }      
            getUsage($scope.type);

        },
        link: function (scope, element, attrs,ctrl) {   
            var chart = false;              
      //        ngModelCtrl.$formatters.push(function(modelValue) {
      //            return modelValue;
            // });

            // ngModelCtrl.$render = function () {

                //scope.ranges = ngModelCtrl.$viewValue;
                console.log(ctrl.ranges);
                var initChart = function() {
                    if (chart) chart.destroy();
                    var config = scope.config || {};
                    chart = AmCharts.makeChart( "speeda_meter", {
                            "type": "gauge",
                            "axes": [ {
                                "axisThickness": 0,
                                "axisAlpha": 0.2,
                                "tickAlpha": 0.2,
                                "valueInterval": 425,   
                                "inside": false,
                                "fontSize": 11,
                                "gridInside": true,
                                "startAngle": -90,
                                "endAngle": 90,
                                "bands": scope.ranges,
                                "topText": "497",
                                "topTextYOffset": 105,
                                "topTextColor": "#555555",
                                "topTextFontSize": 50,  
                                "bottomText": "Watts",
                                "bottomTextYOffset": -10,
                                "bottomTextColor": "#909090",
                                "bottomTextFontSize": 18,
                                "endValue": 1700
                            }],
                            "arrows": [{
                              "startWidth" : 15,
                              "nailBorderThickness" : 1,
                              "nailRadius" : 8 ,
                              "color" : "#5b5b5b",
                            }],
                            "export": {"enabled": true}
                    });         
                };
                initChart();       
           // }             
        }          
    }
});

<gauge ranges="ranges" interval="interval"></gauge>

I'm trying to ranges and interval from the response to assign in the link scope, but it's undefined. 我正在尝试从响应范围到链接范围内的范围和间隔,但这是未定义的。 What is wrong with that? 怎么了

Any solution? 有什么办法吗?

You are trying to assign a value which is coming to you after an asynchronous call. 您正在尝试分配一个值,该值将在异步调用后出现。 When your amhchart is rendered it takes the values for that particular instance and does not give you the ability to two way bind like angular does.Hence when your init function is executed there is no value of $scope.ranges which gets undefined in the chart. 渲染Amhchart时,它会获取特定实例的值,并且无法像angular那样进行双向绑定。因此,当执行init函数时,没有$ scope.ranges值,该值在图表中未定义。

You should render the chart after the call has completed like this 您应该在通话完成后像这样渲染图表

 function getUsage(type){ 
            $http({
                method: 'POST',
                url: apiurl + '/getUsage',
                data: {'type': $scope.type}
            }).success(function (data, status) {
                if (data.status == true) {
                    $scope.ranges = data.result.ranges;
                    $scope.interval = data.result.interval;

                    initChart()//call the chart rendering here
                }    
            });
        }      
        getUsage($scope.type);

or atleast redraw when the call is completed 或通话结束后至少重画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM