wang ming
wang ming

Reputation: 199

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?

Upvotes: 5

Views: 7001

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222722

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.

Upvotes: 11

Related Questions