[英]Get data from scope to another scope within one controller angularjs
我絕對是AngularJS的初學者。 因此,非常感謝您的幫助。 我正在嘗試使用Ionic和Angular-nvD3 lineChart構建帶有圖表的應用程序。 我在json文件中有數據。 因此,我已經建立工廠並使用了getData(),$ scope和service。 我需要一個折線圖,上面有許多數據點。 但是輸出看起來每個點都是彼此分開的。 我以為是因為$ scope和getData函數。 也許其中有某種循環將數據分開。 我正在嘗試重寫代碼以解決問題,將數據從工廠范圍內的數據提取到另一個用於分隔此進程的范圍,但沒有任何運氣。 我在下面的app.js中的代碼
(function() {
var app = angular.module('starter', ['ionic','nvd3']);
app.factory('services', ['$http', function($http){
var serviceBase = 'services/'
var object = {};
object.getData = function(){
return $http.get('chart.json');
};
return object;
}]);
app.controller('MainCtrl', ['$scope', 'services', function($scope, services) {
services.getData().then(function successCb(data) {
$scope.data = _.map(data.data, function(prod) {
var sin = [];
sin.push({
x: prod.Date,
y: prod.low
});
return {
values: sin,
key: 'fff'
}
});
});
$scope.options = {
chart: {
type: 'lineChart',
height: 450,
margin : {
top: 20,
right: 20,
bottom: 40,
left: 55
},
lines: {
xScale: d3.time.scale(),
},
x: function(d){ return d3.time.format('%Y-%m-%d').parse(d.x); },
y: function(d){ return d.y; },
useInteractiveGuideline: true,
dispatch: {
stateChange: function(e){ console.log("stateChange"); },
changeState: function(e){ console.log("changeState"); },
tooltipShow: function(e){ console.log("tooltipShow"); },
tooltipHide: function(e){ console.log("tooltipHide"); }
},
xAxis: {
axisLabel: 'Time (ms)',
tickFormat: function(d){
return d3.time.format('%d-%m-%Y')(d);
},
},
yAxis: {
axisLabel: 'Voltage (v)',
tickFormat: function(d){
return d3.format('.02f')(d);
},
axisLabelDistance: -10
},
callback: function(chart){
console.log("!!! lineChart callback !!!");
}
},
title: {
enable: true,
text: 'Title for Line Chart'
},
subtitle: {
enable: true,
text: 'Subtitle for simple line chart. Lorem ipsum dolor sit amet, at eam blandit sadipscing, vim adhuc sanctus disputando ex, cu usu affert alienum urbanitas.',
css: {
'text-align': 'center',
'margin': '10px 13px 0px 7px'
}
},
caption: {
enable: true,
html: '<b>Figure 1.</b> Lorem ipsum dolor sit amet, at eam blandit sadipscing, <span style="text-decoration: underline;">vim adhuc sanctus disputando ex</span>, cu usu affert alienum urbanitas. <i>Cum in purto erat, mea ne nominavi persecuti reformidans.</i> Docendi blandit abhorreant ea has, minim tantas alterum pro eu. <span style="color: darkred;">Exerci graeci ad vix, elit tacimates ea duo</span>. Id mel eruditi fuisset. Stet vidit patrioque in pro, eum ex veri verterem abhorreant, id unum oportere intellegam nec<sup>[1, <a href="https://github.com/krispo/angular-nvd3" target="_blank">2</a>, 3]</sup>.',
css: {
'text-align': 'justify',
'margin': '10px 13px 0px 7px'
}
}
};
}]);
app.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
});
}());
這是JSON文件中的數據
[
{
"Date": "2011-12-02",
"low": 8758.22466765
},
{
"Date": "2011-12-03",
"low": 8771.50964703
},
{
"Date": "2011-12-04",
"low": 8784.79462641
},
{
"Date": "2011-12-05",
"low": 8798.07960579
},
{
"Date": "2011-12-06",
"low": 8689.04458518
},
{
"Date": "2011-12-07",
"low": 8720.07956456
},
{
"Date": "2011-12-08",
"low": 8718.97454394
},
{
"Date": "2011-12-09",
"low": 8584.72952332
},
{
"Date": "2011-12-10",
"low": 8616.084502700001
},
{
"Date": "2011-12-11",
"low": 8647.43948208
},
{
"Date": "2011-12-12",
"low": 8678.79446147
},
{
"Date": "2011-12-13",
"low": 8552.15944085
},
{
"Date": "2011-12-14",
"low": 8507.64442023
},
{
"Date": "2011-12-15",
"low": 8383.43939961
},
{
"Date": "2011-12-16",
"low": 8388.08437899
},
{
"Date": "2011-12-17",
"low": 8336.42602504
},
{
"Date": "2011-12-18",
"low": 8284.76767109
},
{
"Date": "2011-12-19",
"low": 8233.10931714
},
{
"Date": "2011-12-20",
"low": 8266.49429652
},
{
"Date": "2011-12-21",
"low": 8377.569275900001
},
{
"Date": "2011-12-22",
"low": 8308.55425529
},
{
"Date": "2011-12-23",
"low": 8319.82173467
},
{
"Date": "2011-12-24",
"low": 8331.08921405
},
{
"Date": "2011-12-25",
"low": 8342.35669343
},
{
"Date": "2011-12-26",
"low": 8353.62417281
}
]
我正在嘗試這樣的事情:
services.getData().then(function successCb(data) {
$scope.data = _.map(data.data);
});
$scope.selectedSin = function(prod) {
var sin = [];
angular.forEach(data, function (sin) {
sin.push({
x: data.Date,
y: data.low
});
return {
values: sin,
key: 'fff'
}
});
};
但是控制台中有一個錯誤:
e.values未定義
services.getData().then(function successCb(data) {
$scope.data = _.map(data.data);
});
$scope.selectedSin = function(prod) {
var sin = [];
angular.forEach(data, function (sin) {
sin.push({
x: data.Date,
y: data.low
});
return {
values: sin,
key: 'fff'
}
});
};
在上面的代碼段中, 數據從何處傳遞到angular.forEach?
我在Plunker的注釋中找到了一個很好的答案:將JSON數據加載到Angular-nvD3圖(AngularJS)中,我剛剛將圖表名稱更改為lineChart,將ID和STOCK更改為我的。 柱塞
services.getData().then(function successCb(data) {
$scope.barData = [];
var stock = {
key: 'Product stock',
values: []
};
stock.values = _.map(data.data, function(prod) {
return {
label: prod.Date,
value: prod.low
};
});
console.log(stock);
$scope.barData.push(stock);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.