我试图获得一个角指令元素,它创建了一个d3图表(类似于这里看到的http://phloxblog.in/angulard3/start.html#.Vd5w_bM2w_t),以在事件发生后改变其数据源。
目前,我有一个静态数据源正常工作的指令,但我需要能够对用户定义的事件作出反应,导致从API获取数据并重新加载图表。实际上,我想要这样做:
<myDirective dataSource={{some $scope variable here}} otherStuff=... />
但是这似乎不起作用,因为$scope变量没有被求值。
另一个我认为可以工作的替代方法是让指令做这样的事情:
<myDirective ng-model="some $scope variable here" />
使该指令可以访问1 $scope级别的变量,这是一个json对象与我的配置。
如果这是一个基本问题,我很抱歉。我试着弄清楚如何做到这一点,但我不完全确定我需要在Angular术语中搜索什么。
谢谢!
当有一些用户定义的事件时,你可以看到变量的变化。
$scope.$watch (function ($scope) {
// Lets $scope.variable be the variable which gets changed
// when there is a user event
return $scope.variable
}, function () {
//perform your data fetch from API here
// load it into the data source
});
上面的代码必须在你的控制器中。
关于$scope.watch()有一个很好的教程。