Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: how to watch tab selection?

I am using angularjs tab and pane example components

angular.module('components', []).
    directive('tabs', function() {
        return {
            restrict: 'E',
            transclude: true,
            scope: {},
            controller: function($scope, $element) {
                var panes = $scope.panes = [];

                $scope.select = function(pane) {
                    angular.forEach(panes, function(pane) {
                        pane.selected = false;
                    });
                    pane.selected = true;
                }

                this.addPane = function(pane) {
                    if (panes.length == 0) $scope.select(pane);
                        panes.push(pane);
                    }
                },
                template:
                    '<div class="tabbable">' +
                        '<ul class="nav nav-tabs">' +
                            '<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">'+
                                '<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
                            '</li>' +
                        '</ul>' +
                        '<div class="tab-content" ng-transclude></div>' +
                    '</div>',
                replace: true
            };
        }).
    directive('pane', function() {
        return {
            require: '^tabs',
            restrict: 'E',
            transclude: true,
            scope: { title: '@' },
            link: function(scope, element, attrs, tabsCtrl) {
                tabsCtrl.addPane(scope);
            },
            template:
                '<div class="tab-pane" ng-class="{active: selected}" ng-transclude>' +
                '</div>',
            replace: true
        };
    })

SomePage.html

<tabs>
    <pane title="Datos Generales">
        <div ng-include src="'/resources/js/page/running/admin/templates/update-data.html'"></div>
    </pane>
    <pane title="Localización">
        <div ng-include src="'/resources/js/page/running/admin/templates/update-location.html'"></div>
    </pane>
    <pane title="Datos Contacto">
        <div ng-include src="'/resources/js/page/running/admin/templates/update-contacts.html'"></div>
    </pane>
    <pane title="Variantes">
        <div ng-include src="'/resources/js/page/running/admin/templates/update-variants.html'"></div>
    </pane>
</tabs>

In the second pane there is a GoogleMap. I would like to refresh the google map when the second pane is selected.

I don't know how to get selected pane in the controller. I've tried $scope.panes but is undefined

like image 976
qopuir Avatar asked Feb 18 '13 19:02

qopuir


1 Answers

Here are three ways you could solve your problem:

  1. define a method on your controller, specify that method in an attribute on the tabs element, use the '&' syntax on the tabs directive's isolate scope definition to enable the directive to call the specified method when a pane is selected.
  2. define an object on your controller's scope with a "selectedPane" property. Use the '=' sytnax on the tabs directive's isolate scope definition to enable two-way databinding. Set this property whenever a pane is selected.
  3. have the tabs directive $emit an event, and have your controller listen for it using $on.

Update: @qopuir asked for more details about option 1.
Suppose the controller method is something like the following:

$scope.paneChanged = function(pane) {
    $scope.selectedPane = pane
    ...
}

On the tabs element, we'll specify this function with attribute pane-changed:

<tabs pane-changed="paneChanged(selectedPane)">

Then, the tabs directive can use the '&' syntax to call this method:

.directive('tabs', function() {
    return {
        restrict: 'E',
        transclude: true,
        scope: { paneChanged: '&' },
        controller: function($scope, $element) {
            var panes = $scope.panes = [];

            $scope.select = function(pane) {
                angular.forEach(panes, function(pane) {
                    pane.selected = false;
                });
                pane.selected = true;
                $scope.paneChanged({selectedPane: pane});
            }
like image 94
Mark Rajcok Avatar answered Oct 13 '22 22:10

Mark Rajcok