Akram
Akram

Reputation: 137

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>

Upvotes: 1

Views: 558

Answers (2)

bharathk89
bharathk89

Reputation: 1

Instead of having this

ng-controller="LoginCtrl as lgnCtrl"

in html, we can have this in the controller when the route is defined with a controller, for example in the controller, it will go like this

$routeProvider
        .when("/", { templateUrl: "views/abc.html", controller: "LoginCtrl as lgnCtrl", caseInsensitiveMatch: true });

it worked like a charm

the functions in the controller are only called once.

Upvotes: 0

Rana Ahmer Yasin
Rana Ahmer Yasin

Reputation: 537

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

Upvotes: 5

Related Questions