繁体   English   中英

角度指令范围未绑定数据

[英]angular directive scope not binding data

我有一个问题,像这样的代码:

HTML:

<div class="overflow-hidden ag-center" world-data info="target"></div>

JS:

.directive('worldData', ['$interval', function($interval) {
    return {
        scope: {
            chart: '=info'
        },

        template: '<div>{{chart.aaa}}</div>',

        link: function($scope, element, attrs) {

            $scope.target = {'aaa': 'aaa'};

            aaa = $scope.chart;
        }
    }
}])

图表值未定义,模板没有值,但是当我在控制器中声明$ scope.target时,代码有效,为什么?

通常应采用以下模式:

.controller('myController', function($scope){
    $scope.target = {'aaa': 'aaa'}; //In reality, you'd normally load this up via some other method, like $http.
})

.directive('worldData', [function() {
    return {
        scope: {
            chart: '=info'
        },

        template: '<div>{{chart.aaa}}</div>'
     }
}])

-

<div ng-controller="myController">
    <div class="overflow-hidden ag-center" world-data info="target"></div>
</div>

另外,该指令可能负责处理和获取数据,而不传递任何数据给它。 如果您不需要多个位置的数据,则只需要考虑一下。

暂无
暂无

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

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