如何将Angular-Chart.JS数据添加到UI-Router .State Controller?

时间:2015-08-02 12:42:49

标签: javascript angularjs charts angular-ui-router angular-chart

我正在尝试做什么: 我正在使用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);
    };
});

用户界面 - 路由器代码

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'
    });
});

2 个答案:

答案 0 :(得分:1)

在您的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);
    };

}]);

答案 1 :(得分:0)

我无法让伊曼纽尔的代码为我工作,但他带领我走上正确的道路来使用观点。我从这个链接使用UI路由器读取了多个视图:https://scotch.io/tutorials/angular-routing-using-ui-router。几乎立刻,我能够让它发挥作用。

charts.html页面称为ui-view:

<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]
    ];
});

就是这样!