加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 自定义指令中的ng-repeat:语法错误:令牌’$index

发布时间:2020-12-17 17:48:54 所属栏目:安全 来源:网络整理
导读:在AngularJS中,这失败并出现错误: my-directive ng-repeat="foo in foos" foo="foo" my-index="{{$index}}"/ 错误信息: Error: [$parse:syntax] Syntax Error: Token '$index' is unexpected,expecting [:] at column 3 of the expression [{{$index}}] st
在AngularJS中,这失败并出现错误:

<my-directive ng-repeat="foo in foos" foo="foo" my-index="{{$index}}"/>

错误信息:

Error: [$parse:syntax] Syntax Error: Token '$index' is unexpected,expecting [:] at column 3 of the expression [{{$index}}] starting at [$index}}].

这是指令:

app.directive('myDirective',function() {
    return {
        restrict: 'E',scope: { foo: '=',myIndex: '=' },templateUrl: 'directives/myDirective.html'
    };
});

这似乎只是自定义指令的问题.如果我试试这个:

<div ng-repeat="foo in foos" style="padding: {{$index}}px;">
    index == {{$index}}
</div>

解决方法

由于您使用=来声明隔离的范围属性,因此Angular期望一个未插值的属性:

为了将$index值作为内插值更改为@注入:

scope: { foo: '=',myIndex: '@' },

然后使用:

<my-directive ng-repeat="foo in foos" foo="foo" my-index="{{$index}}"/>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读