j丨aⅩ丨ng

angularjs - How can I use the $index inside a ng-repeat to enable a class and show a DIV? - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.1 million programmers, just like you, helping each other.
J it only takes a minute:
I have a set of &li& elements.
&li ng-class="{current: selected == 100}"&
&a href ng:click="selected=100"&ABC&/a&
&li ng-class="{current: selected == 101}"&
&a href ng:click="selected=101"&DEF&/a&
&li ng-class="{current: selected == $index }"
ng-repeat="x in [4,5,6,7]"&
&a href ng:click="selected=$index"&A{{$index}}&/a&
When a user clicks on one of the address elements above then then it should, set the value of selected and show one of the &DIV& elements below:
ng:show="selected == 100"&100&/div&
ng:show="selected == 101"&101&/div&
&div ng-repeat="x in [4,5,6,7]" ng:show="selected == $index"&{{ $index }}&/div&
This works for the first two cases.
When the user clicks ABC then the first &DIV& shows 100 and changes color to red.
When DEF is clicked then 101 shows and DEF changes to red.
However it does not work at all for A0, A1, A2 and A3
When a user clicks A0, A1, A2 or A3 then the correct
does not show, the selected value is not set and the color of ALL the ng-repeat A0,A1,A2 and A3 turn to red.
This is best shown if you look at this Plunker:
Note that at the top I have added {{ selected }} as a debug aid at the top. Also the x in [4,5,6,7] are just meant to simulate a loop. In real life I have this as ng-repeat="answer in modal.data.answers".
Does anyone know how I can set this up so that the li class current is set at the right time and the DIV shows at the right time for the A0, A1, A2 and A3 &li& and &DIV&
3,96636105177
The issue here is that ng-repeat creates its own scope, so when you do selected=$index it creates a new a selected property in that scope rather than altering the existing one. To fix this you have two options:
Change the selected property to a non-primitive (ie object or array, which makes javascript look up the prototype chain) then set a value on that:
$scope.selected = {value: 0};
&a ng-click="selected.value = $index"&A{{$index}}&/a&
Use the $parent variable to access the correct property. Though less recommended as it increases coupling between scopes
&a ng-click="$parent.selected = $index"&A{{$index}}&/a&
5,63411625
As johnnyynnoj mentioned ng-repeat creates a new scope. I would in fact use a function to set the value. See
$scope.setSelected = function(selected) {
$scope.selected =
{{ selected }}
&li ng-class="{current: selected == 100}"&
&a href ng:click="setSelected(100)"&ABC&/a&
&li ng-class="{current: selected == 101}"&
&a href ng:click="setSelected(101)"&DEF&/a&
&li ng-class="{current: selected == $index }"
ng-repeat="x in [4,5,6,7]"&
&a href ng:click="setSelected($index)"&A{{$index}}&/a&
ng:show="selected == 100"&
ng:show="selected == 101"&
&div ng-repeat="x in [4,5,6,7]"
ng:show="selected == $index"&
{{ $index }}
17.9k104653
protected by
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10
on this site (the ).
Would you like to answer one of these
Not the answer you're looking for?
Browse other questions tagged
rev .25841
Stack Overflow works best with JavaScript enabledB?n ?ang s? d?ng trình duy?t IE 8.0 l?i th?i. Vui lòng c?p nh?t phiên b?n m?i ho?c download Chrome ?? s? d?ng ??y ?? tính n?ng c?a Minh Ng?c.
B?m vào ??y ?? t?i Chrome:
Ho?c click ?? ti?p t?c truy c?p您在慧聪网上采购商品属于商业贸易行为。以上所展示的信息由卖家自行提供,内容的真实性、准确性和合法性由发布卖家负责,请意识到互联网交易中的风险是客观存在的。推荐使用,保障您的交易安全!
按字母分类 :
让慧聪网撮合专家为您解决采购难题
您采购的产品:
请输入采购产品
您的手机号码:
请输入手机号码
*采购产品:
请输入采购产品
*采购数量:
请输入采购数量
*采购截止日期:
请输入正确的手机号码
请输入验证码
*短信验证码:
<input id="valid_Code1" maxlength="6" placeholder="请输入验证码" name="VALIDCODE" class="codeInput" onkeyup="this.value=this.value.replace(/\D/g,'')" onkeypress="if(event.keyCode
57) event.returnValue =" type="text">
免费获取验证码
为了安全,请输入验证码,我们将优先处理您的需求!
请输入验证码
发送成功!
慧聪已收到您的需求,我们会尽快通知卖家联系您,同时会派出采购专员1对1为您提供服务,请您耐心等待!
电话:86-021- &&
联系人:方南南&网络运营部
公司名称:上海永进电缆(集团)有限公司
请输入正确的手机号码
请输入验证码
*短信验证码:
免费获取验证码
为了安全,请输入验证码,我们将优先处理您的需求!
请输入验证码
每一份需求都会在24小时内得到行业多家优质供应商报价。
每一份需求的报价供应商工商信用资质都会经过专业人员检验,交易安全有保障。
免费咨询行业专家
免费咨询行业专家
服务主题:
筛选发货地
验证供应商真伪
提供其他优质供应商
采购数量:
用途描述:
成功加入采购单!
当前采购单共3种货品
成功加入采购单!
当前采购单共3种货品
不能购买自己发布的产品!
选中货品中含失效货品,无法完成下单,可能是:
1.货品库存不足
2.货品已过期,或被卖家删除
3.货品不支持在线交易
卖家暂时不在线,留下联系方式,卖家会主动联系您
*我要采购:
我的姓名:
留言内容:

我要回帖

更多关于 ng4j2j a 的文章

 

随机推荐