angular.js - angular封裝echart自適應問題
phpcn_u1582
phpcn_u1582 2017-05-15 17:11:11
0
1
790

自己封裝了一個echarts

angular.module('docms')
.directive('eChart', function () {
        var eChartDirective = {
            restrict:'EA',
            template:'<p></p>',
            replace:true,
            scope:{
                option:'='
            },
            link:function(scope,eles,attrs,ctrl){
                var chart = echarts.init(eles[0]);
                scope.$watch('option',function(newOption){
                    chart = echarts.init(eles[0]);
                    chart.setOption(newOption);
                },true);
                window.addEventListener('resize',function(){
                    chart.resize();
                })



            }
        }
        return eChartDirective;
    })
<p ng-class="{true:'col-sm-12',false:'col-sm-6'}[width]">
    <e-chart option = "option"></e-chart>
</p>   

原來父元素p只佔50%,後續由於ng-class調整至100%,
但是繪圖在50%時已經完成了,現在我只能實現視窗大小調整時的重繪,有沒有什麼辦法監控父元素寬度的變化,在變化時重繪cavans?

phpcn_u1582
phpcn_u1582

全部回覆(1)
小葫芦

謝邀。

這裡可以把導致p的class變化的那個變數傳給eChart組件。

angular.module('docms')
.directive('eChart', function () {
        var eChartDirective = {
            restrict:'EA',
            template:'<p></p>',
            replace:true,
            scope:{
                option:'=',
                forceRender:'='
            },
            link:function(scope,eles,attrs,ctrl){
                var chart = echarts.init(eles[0]);
                scope.$watch('option',function(newOption){
                    chart = echarts.init(eles[0]);
                    chart.setOption(newOption);
                },true);
                scope.$watch('forceRender',function(newOption){
                    chart.resize();
                },true);
                window.addEventListener('resize',function(){
                    chart.resize();
                })
            }
        }
        return eChartDirective;
    })
<p ng-class="{true:'col-sm-12',false:'col-sm-6'}[width]">
    <e-chart option="option" force-render="width"></e-chart>
</p>   
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!