我需要使用 $http.get 在我的控制器中获取一个 json 文件:
module.controller 'SampleMapController', ($http, $scope) ->
$http.get('../../highcharts/mapdata/world.geo.json').
success (data) ->
$scope.mapData = data # works and logs out correctly
并将其传递给一个指令,该指令使用该 json 进行映射:
module.directive 'MapDirective', () ->
require: '?SampleMapController'
templateUrl: '../template.html'
link: ($scope) ->
console.log $scope.mapData # undefined
$scope.$watch 'an.object.that.contains.more.data',
(newValue) ->
chart = new Highcharts.Chart({
chart: {
renderTo: 'container div'
}
# ... use $scope.mapData somewhere in here to render the global map
})
true
但是我没有任何运气访问该 $scope,并且不确定我是否应该将它放在 $rootScope 上,或者事件需要控制器。
我正在生成一个高图表地图link:
我正在寻找的详细解释在抽象的JSBin中。