ng类的angularjs语法错误

angularjs syntax error with ng-class

本文关键字:错误 语法 angularjs 类的 ng      更新时间:2023-09-26

我是angularjs(以及一般编程)的新手,遇到了麻烦。我将跟随一个教程,尝试创建一个表,并在单击时高亮显示特定的行。我的控制台正在记录一个错误,单击该错误后,我会看到这里:语法错误:令牌"undefined"位于表达式[{3}]的第{2}列,从[{4}]开始。控制台中的原始错误为:[$parse:syntax]http://errors.angularjs.org/1.2.23/$parse/syntax?p0=未定义&p1=是%20意外%2C%20预期%20%5B%7D%5D&p2=空&p3=%7Bselected%3A%20%24索引%3D%3selectedRow&p4=%7已选择%3A%20%24索引%3D%3SelectedRow

app.js

app.controller('RestaurantTableController',['$scope', function ($scope){
    $scope.selectedRow = {};
    $scope.directory= [{name:'The Handsome Heifer', cuisine:'BBQ'},
    {name: "Green's Green Greens", cuisine:'Salads'},
    {name:'House of Fine Fish', cuisine: 'Seafood'}];
    $scope.selectRestaurant=function(row){
        console.log('restaurant test');
        $scope.selectedRow = row;
    };
}]);

index.html:

<table ng-controller="RestaurantTableController">
        <tr ng-repeat="restaurant in directory" ng-click ='selectRestaurant($index)' ng-class='{selected: $index==selectedRow'>
            <td>{{restaurant.name}}</td>
            <td>{{restaurant.cuisine}}</td>
        </tr>
    </table>

看起来您错过了ng类值中的右大括号:

ng-class='{selected: $index==selectedRow}'
相关文章: