我有从服务器获取数据并将其发送到控制器的服务:
服务:
publicApp.angularModule.factory('resultService', function ($http) {
return {
getResult: function() {
return $http.get("/Result/GetResult")
.success(function(result) {
return result.data;
}).error(function(result) {
console.log("error" + result);
});
},
};
});
控制器:
publicApp.angularModule.controller('PublicResultCtrl', function ($scope, $location, resultService) {
resultService.getResult().then(function (resultResponse) {
$scope.data = resultResponse.data;
$scope.graph = [];
_.forEach($scope.data.TreningExerciseScores, function(item) {
$scope.graph.push(addDataToGraph(item.Item2, item.Item1));
});
});
var addDataToGraph = function (num, text) {
return {
y: num,
legendText: text,
};
};
});
我有指令应该从控制器获取数据。我这样称呼指令:
<div id="graph" style="width: 200px; height: 200px" canvasjs graphData="graph"></div>
这是我的指令:
publicApp.angularModule.directive('canvasjs', function () {
return {
restrict: 'A',
scope: {data : '=graphData'} ,
link: function (scope, element, attrs) {
scope.$watch('data', function (data) {
console.log(scope.data);
});
}
};
});
但是 scope.data 是未定义的。我知道 $http.get 是异步操作,但不应该 scope.$watch 获取更新?