AngularJS: how to initiate a ng-click on load

255 views Asked by At

We have a widget in ServiceNow where we have buttons that show pre-configured filters. The buttons work great onClick, but we would like the first button to be clicked and the first filter to be applied on load. Is there a way to initiate this on load? We tried using ng-init, but we couldn't get that to work.

<p><strong>Filters:</strong></p>
<button ng-if="options.show_preconfigured_filters=='true'"
        class="btn btn-outline-primary pull-left m-r-sm m-b-sm"
        ng-repeat="filters in data.preconfigured_filters | orderBy : 'order' track by $index"
        ng-click="c.applyFilter(filters);">
   <i class="glyphicon glyphicon-filter m-r-sm"></i>{
   {filters.title}}
</button>
<div class="clearfix"></div>
c.applyFilter = function(filter) {
    $scope.data.filter = filter.filter;
    c.appendQuery($scope.data.filter);
}
1

There are 1 answers

1
georgeawg On

One approach is to compute the ordered list in the controller:

var orderedList = $filter('orderBy')($scope.data.preconfigured_filters, 'order');

c.applyFilter(orderedList[0]);

This executes the same function that a user invokes by clicking the first item.