angular.js - angularjs中相同的方法,如何做到触发只对当前元素有效。
为情所困
为情所困 2017-05-15 17:04:52
0
2
547


大图里三个部分是从后台读取数据然后绑定用ng-repeat出来的,要实现的功能是点击各个部分最右边的小角标,当前部分能展开或者收起,但是由于ng-repeat,连方法名和ng-show='myVar'都是一样样的,会导致点任何一个角标整片收缩或展开;
这段代码是要ng-repeat的底代码,还未做修改成,

$scope.myVar=false;
$scope.toggle=function(){
    this.myVar=!this.myVar;
}
为情所困
为情所困

reply all(2)
習慣沉默

I think you should add track by $index when ng-repeat, and pass $index into toggle(). Declare a myVar array with the same length as the length you need to repeat. Set the values ​​of the array to true and use ng-if="myVar[$index]" to display them at the beginning.
When clicking the expand and shrink button, toggle($index) just inverts the value of the corresponding position in the array.

仅有的幸福

Because you haven’t used ng-repeat yet, it doesn’t work. If you switch to ng-repeat, it will be fine.
In addition, you don’t need to write a toggle, just ng-click="myVar=!myVar"

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!