Я пытаюсь написать директиву для рисования штрих-карт, и я хотел бы, чтобы он перерисовывал диаграмму каждый раз, когда данные меняются. Сейчас у меня это:
/* This directive will create a bar chart based on a dataTable.
* It expects the first column of the data table to contain the labels.
* If more than 2 columns (labels plus more than one value) are supplied,
* all bars generated by values in one row will be grouped together, making
* an easy visual comparison (benchmark).
* If the option overlay is supplied, the odd value columns will be drawn
* underneath the even values (Ex [label, col1, col2, col3, col4], with overlay = true will result
* in col1 and col3 to be drawn underneath col2 and col4
*/
angular.module('kapstok').directive('nBarChart', ['$window', '$location', '$parse', '$timeout',
function($window, $location, $parse, $timeout){
return{
restrict: 'E',
scope: true,
link: function(scope, elm, attrs){
// Read all the different parameters givin in the directive declaration.
// If options are not given, they are replaced by their default values.
var dataGetter = $parse(attrs.data);
var data = dataGetter(scope) || undefined;
if(!data){
throw Error("No (valid) data source specified!");
}
// Redraw the table once the table gets updated. However, usually multiple elements
// of the table will be updated at the same time, while the watch will get triggered
// at each update. To prevent this, we give it a timeout of half a second,
// allowing all the updates to be applied before the table is redrawn.
scope.$watch(function(){ return data.getVersion(); }, $timeout(drawChart, 500));
Затем в моем контроллере я:
controlmodule.controller('PrefscanController',['$http', '$scope', 'DataTable', function($http, $scope, DataTable){
$scope.myData = new DataTable();
$scope.myData.addColumn("string", "label");
$scope.myData.addColumn("number", "survey");
$scope.myData.addColumn("number", "benchmark");
$scope.myData.addColumn("number", "break-it");
$scope.myData.addRow(['test', 20, 10, 4]);
$scope.myData.addRow(['test2', 42, 13, 2]);
$scope.hideBenchmark = function(){
console.log("myData.version = ", $scope.myData.getVersion());
$scope.myData.hideColumn(2);
console.log("myData.version = ", $scope.myData.getVersion());
}
Во всех функциях из DataTable, когда что-то меняется свойством, называемое «версией» увеличивается, что и Я хочу смотреть $ watch.
В моем шаблоне у меня есть кнопка с ng-кликом, которая вызывает функцию hideBenchmark. Эта функция вызывается, обновляется версия dataTable, но, что бы я ни старался, часы не срабатывают.
Я попытался использовать $ scope. $ Appy()/$ scope. $ Digest() в моей функции hideBenchmark, попробовал добавить «true» к часам (искал объектное неравенство), попытался посмотреть 'attrs.data ',' data.version ', и никто из них, похоже, не запускает часы! Я знаю, что могу создать другую переменную в области, независимой от этих данных, и следить за ней и изменять ее каждый раз, когда я меняю данные, но это кажется уродливым и ненужным.
Кто-нибудь знает, ПОЧЕМУ часы не срабатывают, и как я могу заставить его делать то, что я хочу?
С уважением,
Линус
Вы пытались помещать «данные» в область действия своей директивы, вместо того чтобы получать ее из атрибутов? –