Search code examples
javascriptangularjsionic-frameworkangular-ui-routerng-controller

Angular JS controller called twice (ng-controller)


I'm developping an ionic application and when using angular for login my controller is called twice, I've looked through all the others similar questions but didn't find a solution .

The problem is even when I remove the ng-controller="LoginCtrl as lgnCtrl" I get my controller called once but without two-way databinding.

here is my route file :

$stateProvider
.state('login', {
  url: "/login",
  views: {
    'main': {
      templateUrl: "app/user/loginView.html",
      controller: "LoginCtrl",
      controllerAs: "lgnCtrl"
    }
  }
})

  $urlRouterProvider.otherwise('/login');

here is my controller

angular.module('starter.controllers')
.controller('LoginCtrl', LoginCtrl);

function LoginCtrl($state, $storage, $translate, $ionicPopup, LoginService, messageService) {
    var lgnCtrl = this;
    console.log("user dash 1zz");
    return lgnCtrl;
}

and here is my views: loginView.html :

<ion-view view-title="loginView" id="signinBlk">
   <ion-content>
       <div class="list list col span_1_of_2 "  ng-controller="LoginCtrl as lgnCtrl">
   </div>
   </ion-content>
</ion-view>

index.html:

<body ng-app="starter">
  <ion-nav-view name="main"></ion-nav-view>
</body>

Solution

  • if you already define your controller in route you dont need to define controller in html template remove the ng-controller attribute with value form html template then run it will run just once