Search code examples
angularjsangularjs-ng-clickangular-ngmodel

Is it possible to chain instructions in an ng-click?


I've got a couple of sliders I want to pick from a menu. As a consequence, I need to both pick the right slider and reset the first slide to 0. I have a slide model I set to 0 fro the ng-click just before opening the popup, but, unfortunately, slide is not reset to 0. Any idea?

PS: I don't want to move the slide variable to the scope of the controller.

<div ng-init="slide = 0"></div>

<div 
    ng-repeat="s in sliders"
    style="left:{{s.center.x - 70}}px; top:{{s.center.y}}px"
    ng-click="slide = 0; openPopover('#slides-{{s.name}}')"
    >
    {{s.displayName}}
</div>

Solution

  • As ng-repeat creates it's own scope, your current code will set slide to 0 inside these scopes. A quick (and maybe dirty) way is to use $parent to directly reference the parent-scope. E.g:

    ng-click="$parent.slide = 0; openPopover('#slides-{{s.name}}')"