gpt4 book ai didi

angularjs - 将变量传递给 Angular 指令

转载 作者:行者123 更新时间:2023-12-02 19:32:28 26 4
gpt4 key购买 nike

如果我有一个指令 myDir 并且我在 ng-repeat 中调用它,就像这样

<my-dir myindex="{{$index}}"></my-dir>

如何访问myindex?当我在 postLink 函数中使用 attrs.myindex 时,我得到实际的字符串 {{$index}} 。当我检查 html 时,它实际上显示 myindex="2"

最佳答案

尝试

<my-dir myindex="$index"></my-dir>

然后

app.directive('myDir', function () {
return {
restrict: 'E',
scope: {
myindex: '='
},
template:'<div>{{myindex}}</div>',
link: function(scope, element, attrs){
scope.myindex = attrs.myindex;
console.log('test', scope.myindex)
}
};
})

演示:Plunker

关于angularjs - 将变量传递给 Angular 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16432198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com