1

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>

2 Answers 2

5

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

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

13 Comments

But, the problem is when i do that i have no more the two waydata banding: my ng-click is not catched in controller and I cannot get the controller scope
you have because your route config manag all things you have as name no worried
sorry, I didn't understand. I mean, that when I remove the ng-controller, ng-click is not triggered in controller and don't execute my click function . so what to do ?
ok change the state .state('login', { url: "/login", views: { 'main': { templateUrl: "app/user/loginView.html", } } })
dont define the controller in config
|
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.