Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ng-click on md-tab label

I'm having trouble with md-tabs : I can't call a controller function whenever I click on a specific tab to be displayed.

Here are the few codes I tried :

<md-tabs md-center-tabs id="menuTabs" md-dynamic-height="true" ng-click="focusSearch()">

            <md-tab label="<i class='fa fa-search'></i>" ng-if="handleResize()">
                <md-content class="control-sidebar-dark" ng-style="{'height' : menuHeight}">

and

<md-tabs md-center-tabs id="menuTabs" md-dynamic-height="true">

            <md-tab label="<i class='fa fa-search' ng-click='focusSearch()'></i>" ng-if="handleResize()">
                <md-content class="control-sidebar-dark" ng-style="{'height' : menuHeight}">

and

<md-tabs md-center-tabs id="menuTabs" md-dynamic-height="true">

            <md-tab ng-click="focusSearch()" label="<i class='fa fa-search'></i>" ng-if="handleResize()">
                <md-content class="control-sidebar-dark" ng-style="{'height' : menuHeight}">

I want my focusSearch() function to be called when I click on this tab but she never is.

like image 989
Ellone Avatar asked Aug 03 '15 09:08

Ellone


2 Answers

Try using these built in md-tab attributes

  • md-on-select
  • md-on-deselect

var app = angular.module('myapp', ['ngMaterial']);

app.controller('main', function($scope) {

  $scope.myClickEvent1 = function() {
    alert('one selected');
  }

  $scope.myClickEvent2 = function() {
    alert('two selected');
  }

});
<link href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/0.11.0/angular-material.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular-aria.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular-animate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-material/0.11.0/angular-material.min.js"></script>

<div ng-app="myapp" ng-controller="main">
  <md-tabs>
    <md-tab md-on-select="myClickEvent1()">One</md-tab>
    <md-tab md-on-select="myClickEvent2()">Two</md-tab>
  </md-tabs>
</div>

Note: This event differs slightly from ng-click, in that if a tab is already selected and then clicked, the event will not fire.

Source: https://material.angularjs.org/latest/#/api/material.components.tabs/directive/mdTab

like image 135
Trevor Avatar answered Oct 16 '22 01:10

Trevor


Like Creator suggested, I ended up using a directive instead of ng-click :

http://codepen.io/anon/pen/oXJPvx

like image 43
Ellone Avatar answered Oct 15 '22 23:10

Ellone