如何将 Angular-Chart.JS 数据添加到 UI-Router .State Controller?
How do I add Angular-Chart.JS data into UI-Router .State Controller?
我正在尝试做的事情:
我正在使用 Angular UI-Router 构建单页应用程序。我的其中一页有折线图,所以我使用 angular-chart.js.
不明白的地方:
我不明白如何让图表显示在 UI-Router 状态中。如果我不将它包含在单页应用程序中,我可以让图表工作。我觉得我需要向包含 $scope 标签、系列和数据的 UI-Router 状态添加一个控制器,但我无法让它正常工作。
Angular-Chart.js代码
var chartApp = angular.module('myApp', ['chart.js']);
chartApp.controller("LineCtrl", function ($scope) {
'use strict';
$scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
$scope.series = ['Motivation', 'Workload'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
$scope.onClick = function (points, evt) {
console.log(points, evt);
};
});
UI-路由器代码
var myApp = angular.module('myApp', ['ui.router']);
myApp.config(function($stateProvider, $urlRouterProvider) {
'use strict';
// For any unmatched url, redirect home
$urlRouterProvider.otherwise('/home');
$stateProvider
// chart page
.state('charts', {
url: '/charts',
templateUrl: 'app/charts.html'
});
});
在您的 javascript 代码中,您没有包含控制器。应该是:
var myApp = angular.module('myApp', ['ui.router']);
myApp.config(function($stateProvider, $urlRouterProvider) {
'use strict';
// For any unmatched url, redirect home
$urlRouterProvider.otherwise('/home');
$stateProvider
// chart page
.state('charts', {
url: '/charts',
templateUrl: 'app/charts.html'
controller: 'LineCtrl'
});
});
不过,还有比这更好的方法。您可以解析来自服务的数据,以便页面在显示已编译的 html.
之前等待该数据
var myApp = angular.module('myApp', ['ui.router', 'someService']);
myApp.config(function($stateProvider, $urlRouterProvider) {
'use strict';
// For any unmatched url, redirect home
$urlRouterProvider.otherwise('/home');
$stateProvider
// chart page
.state('charts', {
url: '/charts',
//I use the 'views' property to ready my code for nested views as well as to
//isolate the properties for each view on that particular url
views: {
'':{
templateUrl: 'app/charts.html',
controller: 'LineCtrl',
controllerAs: 'lineCtrl',
resolve:{
labels: function(someService){
return someService.getAllLabels();
},
series: function(someService){
return someService.getAllSeries();
},
data: function(someService){
return someService.getAllData();
}
}
}
}
});
});
你的控制器应该是这样的:
var chartApp = angular.module('myApp', ['chart.js']);
chartApp.controller("LineCtrl", ['$scope', 'labels', 'series', 'data', function ($scope, labels, series, data) {
'use strict';
$scope.labels = labels;
$scope.series = series;
$scope.data = data;
$scope.onClick = function (points, evt) {
console.log(points, evt);
};
}]);
我无法让 Immanuel 的代码为我工作,但他引导我走上了使用视图的正确轨道。我使用 UI 路由器从这个 link: https://scotch.io/tutorials/angular-routing-using-ui-router 阅读了多个视图。几乎是立刻,我就能让它工作了。
charts.html 页面调用了 ui-视图:
<div class="row">
<div class="col-md-6 col-sm-12" id="line-chart" ui-view="line-chart"></div>
<div class="col-md-6 col-sm-12" id="bar-chart" ui-view="bar-chart"></div>
</div>
<div class="row">
<div class="col-md-6 col-sm-12" id="doughnut-chart" ui-view="doughnut-chart"></div>
<div class="col-md-6 col-sm-12" id="radar-chart" ui-view="radar-chart"></div>
</div>
我的app.js代码:
var myApp = angular.module('myApp', ['ui.router', 'chart.js']);
myApp.config(function($stateProvider, $urlRouterProvider) {
'use strict';
// For any unmatched url, redirect home
$urlRouterProvider.otherwise('/home');
$stateProvider
// charts page
.state('charts', {
url: '/charts',
views: {
'': { templateUrl: 'app/charts.html', },
'line-chart@charts': {
templateUrl: 'app/charts-line.html',
controller: 'lineController'
},
"bar-chart@charts": {
templateUrl: 'app/charts-bar.html',
controller: 'barController'
},
"doughnut-chart@charts": {
templateUrl: 'app/charts-doughnut.html',
controller: 'doughnutController'
},
"radar-chart@charts": {
templateUrl: 'app/charts-radar.html',
controller: 'radarController'
}
}
})
});
然后我给每一个控制器都打了电话。这是折线图:
// Define the charts controller called from the charts state
myApp.controller('lineController', function($scope) {
'use strict';
$scope.message = 'There should be a line chart here:';
$scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
$scope.series = ['Motivation', 'Workload'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
});
就是这样!
我正在尝试做的事情: 我正在使用 Angular UI-Router 构建单页应用程序。我的其中一页有折线图,所以我使用 angular-chart.js.
不明白的地方: 我不明白如何让图表显示在 UI-Router 状态中。如果我不将它包含在单页应用程序中,我可以让图表工作。我觉得我需要向包含 $scope 标签、系列和数据的 UI-Router 状态添加一个控制器,但我无法让它正常工作。
Angular-Chart.js代码
var chartApp = angular.module('myApp', ['chart.js']);
chartApp.controller("LineCtrl", function ($scope) {
'use strict';
$scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
$scope.series = ['Motivation', 'Workload'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
$scope.onClick = function (points, evt) {
console.log(points, evt);
};
});
UI-路由器代码
var myApp = angular.module('myApp', ['ui.router']);
myApp.config(function($stateProvider, $urlRouterProvider) {
'use strict';
// For any unmatched url, redirect home
$urlRouterProvider.otherwise('/home');
$stateProvider
// chart page
.state('charts', {
url: '/charts',
templateUrl: 'app/charts.html'
});
});
在您的 javascript 代码中,您没有包含控制器。应该是:
var myApp = angular.module('myApp', ['ui.router']);
myApp.config(function($stateProvider, $urlRouterProvider) {
'use strict';
// For any unmatched url, redirect home
$urlRouterProvider.otherwise('/home');
$stateProvider
// chart page
.state('charts', {
url: '/charts',
templateUrl: 'app/charts.html'
controller: 'LineCtrl'
});
});
不过,还有比这更好的方法。您可以解析来自服务的数据,以便页面在显示已编译的 html.
之前等待该数据var myApp = angular.module('myApp', ['ui.router', 'someService']);
myApp.config(function($stateProvider, $urlRouterProvider) {
'use strict';
// For any unmatched url, redirect home
$urlRouterProvider.otherwise('/home');
$stateProvider
// chart page
.state('charts', {
url: '/charts',
//I use the 'views' property to ready my code for nested views as well as to
//isolate the properties for each view on that particular url
views: {
'':{
templateUrl: 'app/charts.html',
controller: 'LineCtrl',
controllerAs: 'lineCtrl',
resolve:{
labels: function(someService){
return someService.getAllLabels();
},
series: function(someService){
return someService.getAllSeries();
},
data: function(someService){
return someService.getAllData();
}
}
}
}
});
});
你的控制器应该是这样的:
var chartApp = angular.module('myApp', ['chart.js']);
chartApp.controller("LineCtrl", ['$scope', 'labels', 'series', 'data', function ($scope, labels, series, data) {
'use strict';
$scope.labels = labels;
$scope.series = series;
$scope.data = data;
$scope.onClick = function (points, evt) {
console.log(points, evt);
};
}]);
我无法让 Immanuel 的代码为我工作,但他引导我走上了使用视图的正确轨道。我使用 UI 路由器从这个 link: https://scotch.io/tutorials/angular-routing-using-ui-router 阅读了多个视图。几乎是立刻,我就能让它工作了。
charts.html 页面调用了 ui-视图:
<div class="row">
<div class="col-md-6 col-sm-12" id="line-chart" ui-view="line-chart"></div>
<div class="col-md-6 col-sm-12" id="bar-chart" ui-view="bar-chart"></div>
</div>
<div class="row">
<div class="col-md-6 col-sm-12" id="doughnut-chart" ui-view="doughnut-chart"></div>
<div class="col-md-6 col-sm-12" id="radar-chart" ui-view="radar-chart"></div>
</div>
我的app.js代码:
var myApp = angular.module('myApp', ['ui.router', 'chart.js']);
myApp.config(function($stateProvider, $urlRouterProvider) {
'use strict';
// For any unmatched url, redirect home
$urlRouterProvider.otherwise('/home');
$stateProvider
// charts page
.state('charts', {
url: '/charts',
views: {
'': { templateUrl: 'app/charts.html', },
'line-chart@charts': {
templateUrl: 'app/charts-line.html',
controller: 'lineController'
},
"bar-chart@charts": {
templateUrl: 'app/charts-bar.html',
controller: 'barController'
},
"doughnut-chart@charts": {
templateUrl: 'app/charts-doughnut.html',
controller: 'doughnutController'
},
"radar-chart@charts": {
templateUrl: 'app/charts-radar.html',
controller: 'radarController'
}
}
})
});
然后我给每一个控制器都打了电话。这是折线图:
// Define the charts controller called from the charts state
myApp.controller('lineController', function($scope) {
'use strict';
$scope.message = 'There should be a line chart here:';
$scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
$scope.series = ['Motivation', 'Workload'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
});
就是这样!