[英]Angularjs Linechart directive won't load $resource
這是資源
hq.factory('AvgUsersPerWeek', function ($resource) {
return $resource('/HQ/Graph/GetLoggedinUsersPerWeek')
});
控制器
hq.controller('AvgUsersPerWeekCtrl', function ($scope, AvgUsersPerWeek) {
$scope.xkey = 'Period';
$scope.ykeys = ['CountUsers'];
$scope.labels = ['Aantal'];
$scope.myModel = AvgUsersPerWeek.query();
/*
//example dataset that works, the fetched data has the same structure
[
{ "Period": "2013-04", "CountUsers": 2407 },
{ "Period": "2013-03", "CountUsers": 3351 },
{ "Period": "2013-02", "CountUsers": 2469 },
{ "Period": "2013-01", "CountUsers": 2246 },
{ "Period": "2012-12", "CountUsers": 3171 },
{ "Period": "2012-11", "CountUsers": 2155 },
{ "Period": "2012-10", "CountUsers": 1226 },
{ "Period": "2012-09", "CountUsers": 2245 }
]; */
})
僅供參考。 如果我用下面的數據集替換AvgUsersPerWeek.query(),它將起作用。 因此,我的資源出現了問題(獲取了資源(Fiddler)。但是該值並未插入到我的圖表中。
我已經仔細檢查過,示例數據集與$ resource獲取的數據集具有相同的結構。
我沒有附上HTML,因為它可以工作。 我想我使用$ resources的方式有些問題。
我猜您在資源中缺少回調。 您應該在控制器中執行以下操作:
AvgUsersPerWeek.query(function(data) {
$scope.myModel = data;
});
查看此頁面以獲取更多信息
在您的指令中,數據可能未更新,您應該在指令中添加觀察者(或觀察屬性),以根據資源的變化來更新數據
$attrs.$observe('data', function(newVal){
console.log(newVal);
// The rest of your link
})
這是您的jsfiddle的更新版本
問題作者更新:我將指令更改為使用:
$scope.$watch(attrs.data, function (newVal) { ...
而且有效,謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.