角度指令范围不绑定数据

时间:2015-05-15 02:10:35

标签: javascript angularjs angularjs-directive

我有一个问题,代码如下:

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时,代码有效,为什么?

1 个答案:

答案 0 :(得分:1)

这通常应该是模式:

private volatile List<int> _list;

public void Process() {
    try {
         // This is an acquire, because we're *reading* from a volatile field.
        _list.Add(42);
    } finally {
        // This is a release, because we're *writing* to a volatile field.
        _list = _list;
    }
}

-

.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>'
     }
}])

或者,指令可以负责获取和获取数据,而不是传递任何数据。如果您不需要多个地方的数据,您只需要考虑这一点。

相关问题