ng-change directive triggered before model is updated when clicking uib-button-radio element

Here’s a plnkr:

I’m using uib-button-radio elements to toggle the active filter on an ng-repeat.

  <div ng-controller="ButtonsCtrl as $ctrl">
    <pre>Model: {{$ctrl.radioModel}}</pre>
    <div class="btn-group">
      <label class="btn btn-primary" ng-change="$" ng-model="$ctrl.radioModel" uib-btn-radio="'Left'">Left</label>
      <label class="btn btn-primary" ng-change="$" ng-model="$ctrl.radioModel" uib-btn-radio="'Middle'">Middle</label>
      <label class="btn btn-primary" ng-change="$" ng-model="$ctrl.radioModel" uib-btn-radio="'Right'">Right</label>

    <p ng-repeat="item in $ctrl.filtered = ($ctrl.collection | filter: { Value: $ctrl.radioModel } )">{{item | json}}</p>

    <p>First filtered item: {{$ | json}}</p>

ng-change/ng-click function: = function() { = ctrl.filtered[0];

When the selected filter changes, and subsequently the collection is updated, I want to select the first item in the filtered collection. However it appears that the ng-change/ng-click function is firing before the collection is updated by the filter.


What you can do is apply the filtering logic in JavaScript instead of in the markup.

In the HTML, change the ng-repeat expression to just:

<p ng-repeat="item in $ctrl.filtered">{{item | json}}</p>

In the controller, inject $filter:

angular.module('ui.bootstrap.demo').controller('ButtonsCtrl', function ($scope, $filter) {

Initialize the filtered array:

ctrl.filtered = [];

And do the filtering in the do change handler: = function() {
    ctrl.filtered = $filter('filter')(ctrl.collection, { Value: ctrl.radioModel }); = ctrl.filtered[0];