Get Current Item from ng-repeat

34 views
Skip to first unread message

Olivier DUBOIS

unread,
Nov 19, 2015, 5:23:58 AM11/19/15
to AngularJS
Hi,

I would like to get current item from ng-repeat (like ng-model) 


<div data-ng-controller="WidgetCtrl">

    <ul>

        <li data-ng-repeat="widget in widgets">

            ID {{widget.ID}}: {{widget.Color}} {{widget.Shape}}

        </li>

    </ul>

</div>


<div>Current Widget = <span ng-bind="{{widget.ID}}"></span></div>


How to update (observable)  the Current Widget


Regards

Vaibhav Gupta

unread,
Nov 20, 2015, 1:59:23 AM11/20/15
to AngularJS
Hi,

Can you elaborate on what is meant by "Current Widget". Any widget can be accessed using {{widgets[index]}}.

Regards,

Vaibhav Gupta

Gora LEYE

unread,
Nov 20, 2015, 5:11:51 AM11/20/15
to ang...@googlegroups.com

Hi. Thanks you. I would like to retrive Current item and display it outside of looping.
Regards.

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/Rkc0tXZhpWk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
Nov 20, 2015, 5:49:54 AM11/20/15
to AngularJS
Hi Olivier,

Switch to controllerAs syntax and do try something like:

<div data-ng-controller="WidgetCtrl as wdgt">
   
<ul>
       
<li data-ng-repeat="widget in widgets" ng-click='wdgt.active = widget>

            ID {{widget.ID}}: {{widget.Color}} {{widget.Shape}}
       
</li>
   
</ul>


   
<div>Current Widget = <span ng-bind="{{wdgt.active.ID}}"></span></div>

</div>



Hope this helps a bit ;)

regards

Sander


Gora LEYE

unread,
Nov 20, 2015, 7:51:14 AM11/20/15
to ang...@googlegroups.com
Hi,
What I want to do is something like this (c#):

           string CurrentWidget;

            foreach (var widget in widgets)
            {
                CurrentWidget = widget;
            }

So, CurrentWidget is outside of loop, and I like to do  CurrentWidget = widget  where widget is current element.

DataBinding  ==>  widget and CurrentWidget

Mrs LEYE Gora
Tocane Technologies
7, avenue des ursulines 78300 POISSY
512 987 777 R.C.S. VERSAILLES
TEL : 06 98 33 33 85
Site Web : 

Sander Elias

unread,
Nov 20, 2015, 7:56:07 AM11/20/15
to AngularJS
Hi Olivier,

this makes no sense to me:

foreach (var widget in widgets)
           
{
               
CurrentWidget = widget;
           
}

What are you expecting your CurrentWidget to be? In practice it will always be the last one, is that what you are looking for?
Also, this is exactly what I did in my previous sample, but I did the assignment on a click event, so your user can pick what 'widget' he want's to interact with.

Regards
Sander

Harinath Mallepally

unread,
Nov 20, 2015, 2:45:55 PM11/20/15
to AngularJS
Yes,

Oliver, are you sure about what you really want to achieve? I think you want to display the currently selected item somewhere in the page, when you want to do that, you have nothing to do with ng-repeat.

I think you need to write a function in controller and add that to the individual item (on click or something) and then you change your current item in a scope variable (that should be bound with the item you have for display for current item in the page).

If I got you wrong, please explain what you want to do.

Thanks
Hari
Reply all
Reply to author
Forward
0 new messages