我想從我的控制器裏面檢索jsonp數據。我要搶在$ http.jsonp從URL一些JSONP數據,並使之通過,通過數據環路成功的功能,然後將其推到一個變量dataxx但我不斷收到此錯誤:AngularJS http.jsonp回調沒有定義
從Uncaught ReferenceError: myJsonMethod is not defined
documentation
angular.module('app', ['onsen'])
.controller('ChartController', ['$scope', '$http', function($scope,$http) {
this.data = [{
key: 'Data',
values: []
}];
$http.jsonp("https://demo8162910.mockable.io/json?callback=myJsonMethod").
success(function(data, status, headers, config) {
//what do I do here?
dataxx.push({"x":9,"y":11},{"x":9,"y":18});
}).
error(function(data, status, headers, config) {
$scope.error = true;
});
$scope.data = [{
key: 'Data',
values: dataxx
}];
}])
.factory('d3', [function() {
return d3;
}])
.factory('nv', [function() {
return nv;
}])
.directive('lineChart', ['d3', 'nv', function(d3, nv) {
return {
restrict: 'E',
scope: {
data: '=',
height: '@',
width: '@'
},
template: '<svg ng-attr-height="{{ height }}" ng-attr-width="{{ width }}"></svg>',
link: function(scope, element) {
var svg = element.find('svg'),
chart;
var update = function() {
d3.select(svg[0])
.datum(scope.data)
.call(chart);
};
scope.$watch(function() { return angular.toJson(scope.data); }, function() {
if (chart) {
update();
}
});
scope.$on('chartloaded', update);
nv.addGraph(function() {
chart = nv.models.lineChart()
.showLegend(false)
.showYAxis(true)
.showXAxis(true);
chart.xAxis
.axisLabel('x')
.tickFormat(d3.format('.2f'));
chart.yAxis
.axisLabel('y')
.tickFormat(d3.format('.2f'));
nv.utils.windowResize(function() {
chart.update()
});
scope.$emit('chartloaded');
return chart;
});
}
}
}]);
http://stackoverflow.com/questions/12066002/parsing-jsonp-http-jsonp-response-in-angular-js – AlainIb
你定義了函數myJsonMethod嗎?如果不是,你可以使用'JSON_CALLBACK' – maurycy