简体   繁体   English

角度指令中的观察者未触发

[英]watcher in angular directive not triggering

I created my own directive and watching an attribute like this: 我创建了自己的指令,并观看了这样的属性:

angular.module('app', [])
.controller('pieController', ['$scope', function ($scope) {

    function initData() {
        var arr = [];
        for (var i = 0; i < 6; i++) {
            arr.push(parseInt(Math.random() * 100));
        }
        return arr;
    }

    var data = initData();
    $scope.data = data;

    $scope.changData = function () {
        var data = initData();
        $scope.data = data;
    }

}]).directive('eChart', [function () {

    function link($scope, element, attrs) {

        var myChart = echarts.init(element[0]);

        if (attrs.myOptions) {
            $scope.$watch(attrs.myOptions, function () {
                var options = $scope.$eval(attrs.myOptions);
                if (angular.isObject(options)) {
                    myChart.setOption(options);
                }
            }, true);
        }
    }

    return {
        restrict: 'A',
        link: link
    };
}]);

and the html is like this: 和HTML是这样的:

<div class="col-xs-12" ng-controller="pieController">
        <button ng-click="changData()">click me</button>
        <div e-chart my-options="{tooltip: {show: true},
        legend: {
            data: ['销量']
        },
        xAxis: [
            {
                type: 'category',
                data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        }
        ],
        yAxis: [
        {
        type: 'value'
        }
        ],
        series: [
        {
        'name': '销量',
        'type': 'bar',
        'data': {{data}}
        }
        ]}" style="height: 400px;width: 100%;"></div>
    </div>
</div>

In my controller,i change the value of the attribute,but the watcher doesn't trigger. 在我的控制器中,我更改了属性的值,但观察者未触发。

So what i am doing wrong?Thanks. 那我在做什么错呢?

here is code in fiddle 这是小提琴中的代码

I thought you should bind the data directly. 我认为您应该直接绑定数据。

 app.directive('eChart', [function() { function link($scope, element, attrs) { if (attrs.myOptions) { $scope.$watch('data', function(newValue, oldValue) { if (angular.isObject(newValue)) { console.log('change'); } }, true); } } return { restrict: 'A', scope: { data: '=myData' }, link: link }; }]); 
 <div e-chart my-data="data" my-options="... your options ..."></div> 

Please use attrs.$observe You directive should be like this 请使用attrs.$observe你的指令应该像这样

 .directive('eChart', [function () {
    function link($scope, element, attrs) {

        element.text("innerText");

        //监听options变化
        if (attrs.myOptions) {
            attrs.$observe('myOptions', function () {
                console.log("变了")
                var options = $scope.$eval(attrs.myOptions);
                element.text(attrs.myOptions);
            })
        }
    }

    return {
        restrict: 'A',
        link: link
    };
}]);

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

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