Я пытаюсь использовать ng-switch с ng-include ниже. Проблема заключается в том, что ng-init и весь блок контроллера получают повторное отображение любого изменения ng-include.
В login_form.html, когда пользователь входит в систему, я устанавливаю isLoggedIn = true в LoginCtrl. Однако это приводит к повторному рендерингу полного html ниже, что снова вызывает ng-init.
Как избежать этого цикла?
      <div ng-controller="LoginCtrl" ng-init="isLoggedIn = false" class="span4 pull-right">
        <div ng-switch on="isLoggedIn"> 
          <div ng-switch-when="false" ng-include src="'login_form.html'"></div>
          <div ng-switch-when="true" ng-include src="'profile_links.html'"></div>
        </div>
      </div>
Ниже приведен HTML-код для входа в систему -
<form class="form-inline">
  <input type="text" placeholder="Email" ng-model="userEmail" class="input-small"/>
  <input type="password" placeholder="Password" ng-model="userPassword" class="input-small"/>
  <button type="submit" ng-click="login(userEmail, userPassword)" class="btn">Sign In</button>
</form>
Ниже находится контроллер -
angularApp.controller('LoginCtrl', function($scope, currentUser){
  $scope.loginStatus = function(){
    return currentUser.isLoggedIn();
  };
/*  $scope.$on('login', function(event, args) {
    $scope.userName = args.name;
  }); 
  $scope.$on('logout', function(event, args) {
    $scope.isLoggedIn = false;
  });*/
  $scope.login = function(email, password){
    currentUser.login(email, password);
  };
  $scope.logout = function(){
    currentUser.logout();
  };
});
Удар - это сервис -
angularApp.factory('currentUser', function($rootScope) {
  // Service logic
  // ...
  // 
    var allUsers = {"[email protected]": {name: "Robert Patterson", role: "Admin", email: "[email protected]", password: "rob"},
            "[email protected]":{name: "Steve Sheldon", role: "User", email: "[email protected]", password: "steve"}}
  var isUserLoggedIn = false;
  // Public API here
  return {
    login: function(email, password){
      var user = allUsers[email];
      var storedPass = user.password;
      if(storedPass === password){
        isUserLoggedIn = true;
        return true;
      }
      else
      {
        return false;
      }
    },
    logout: function(){
      $rootScope.$broadcast('logout');
      isUserLoggedIn = false;
    },
    isLoggedIn: function(){
      return isUserLoggedIn;
    }
 };
});


