2

My controller gets called twice when I am using angular material tabs.

The tabs:

<div layout="column" flex="">
  <md-content id="content">
    <md-tabs md-dynamic-height="" md-selected="selectedIndex" md-border-bottom="" md-autoselect="">
      <md-tab label="Home" ui-sref="home">
        <div ui-view=""></div>
      </md-tab>
      <md-tab label="Contact" ui-sref="contact">
        <div ui-view=""></div>
      </md-tab>
    </md-tabs>
  </md-content>
</div>

The controllers:

(function () {
    'use strict';

    angular
        .module('app.contact')
        .controller('ContactController', ContactController);

    function ContactController() {
        var vm = this;

        activate();

        function activate() {
          console.log('Contact Controller');
        }

    }
})();

(function () {
    'use strict';

    angular
        .module('app.home')
        .controller('HomeController', HomeController);

    function HomeController() {
        var vm = this;

        activate();

        function activate() {
          console.log('Home Controller');
        }

    }
})();

The states:

(function () {
    'use strict';

    angular.module('app', [
    ])
        .config(configBlock);

    function configBlock($stateProvider, $urlRouterProvider) {
      $urlRouterProvider.otherwise("/home");

      $stateProvider
        .state('home', {
            url: "/home",
            templateUrl: "home.html",
            controller: 'HomeController',
            controllerAs: 'vm'
        })
        .state('contact', {
            url: "/contact",
            templateUrl: "contact.html",
            controller: 'ContactController',
            controllerAs: 'vm'
        });
    }

})();

Plunker

2 Answers 2

1

This is, because you are using ui-view two times.

Sign up to request clarification or add additional context in comments.

1 Comment

Even if <ui-view> was used once, the controller would have been called twice. I am also encountering this problem. Looking for some good explanation and solution to this issue.
0

i also hav the same issue use the ng-include instead of the ng-view

<md-tab label="Issues" >   
    <ng-include src="'/app/src/project/task/task.tpl.html'"></ng-include>
    <!-- <div ui-view></div> -->
</md-tab>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.