Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Error: Token '{' invalid key at column 2 of the expression [{{id}}] starting at [{id}}]?

This is my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="js/angular.js"></script>
    <script src="js/Chart.min.js"></script>
    <script src="js/angular-chart.js"></script>
</head>
<body>

<div ng-app="app">
    <div ng-controller="BarCtrl">
        <div ng-repeat="id in ids">
            <canvas id="bar" class="chart chart-bar"
                    chart-data="{{id}}" chart-labels="labels"> chart-series="series"
            </canvas>

        </div>
    </div>
</div>

<script type="application/javascript">

    angular.module("app", ["chart.js"]).controller("BarCtrl", function ($scope) {
        $scope.labels = ['2008', '2009', '2010', '2011', '2012'];
        $scope.series = ['Series A', 'Series B'];

        $scope.data = [];
        $scope.data.push( [
            [65, 59, 80, 81, 56, 55, 40],
            [28, 48, 40, 19, 86, 27, 90]
        ]);

        $scope.ids = ["data[0]"];
    });

</script>
</body>
</html>

When I run the code in Chrome, the console wrote:

[$parse:syntax] Syntax Error:
    Token '{' invalid key at column 2 of the expression [{{id}}] starting at [{id}}].

<input value="{{id}}"> seems to be ok, but chart-data="{{id}}" is causing the Syntax Error. Any idea why that would happen?

like image 221
wang ming Avatar asked Feb 15 '17 04:02

wang ming


1 Answers

chart-data should not have expression, it should have a scope variable binding.

<canvas id="bar" class="chart chart-bar" chart-data="data" chart-labels="labels" chart-series="series">
</canvas>

{{ ... }} means you're evaluating an expression, for example {{1+1}} or printing (not passing) a variable {{id}}. However, to pass a variable, you should not wrap it in an expression when you're assigning it to a property.

like image 117
Sajeetharan Avatar answered Oct 15 '22 02:10

Sajeetharan