AngularJs:如何将contorller范围变量访问到指令

时间:2017-09-25 07:16:13

标签: angularjs-directive

我正在制作实时d3可视化图表,但无法将控制器范围访问为指令。

var app = angular.module('myChart', ['ngMaterial', 'ngMessages']);

app.controller('chartCtrl', function($scope, $http, $mdDialog, $window){

// Get chart setting
$http.get('chart_setting.php').then(function(res){
    $scope.info = res.data;
    $scope.me = 'hello';
});

});

app.directive('motionChart', function(){
return {
    restrict: 'E',
    link: link,
    scope: {info: "="}
}

function link(scope, el, attr){
    console.log(scope.info);// Here is info data in json 
    console.log(scope.me);// Try to fetch simple string but not success
}

});

在指令中,我需要" info"和"我"范围变量。但无法访问。

如果我控制范围,那么数据就像

一样

print whole scope

范围打印成功,但当我获得scope.anyVariable时,我得到了未定义。

由于

1 个答案:

答案 0 :(得分:0)

bool CameraFrameGrabber::present(const QVideoFrame &frame)
{

    qDebug() << __FUNCTION__;
    if (frame.isValid()) {
        QVideoFrame cloneFrame(frame);
        cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
        const QImage image(cloneFrame.bits(),
            cloneFrame.width(),
            cloneFrame.height(),
            QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
        if (MainWindow* child = dynamic_cast<MainWindow*>(this)) {
            //QGraphicsScene *scene = new QGraphicsScene(this);
            //scene->addPixmap(QPixmap::fromImage(image));
            //scene->setSceneRect(image.rect());
            child->ui->label->setPixmap(QPixmap::fromImage(image));

            child->ui->label->update();
            //child->ui->graphicsView->setScene(scene);
            //child->ui->graphicsView->update();

        }
        //emit frameAvailable(image);
        cloneFrame.unmap();
        return true;
    }
    return false;

}