0

I have checked some of the topics for this matter and i got an understanding of controllers are there to initiate scope and i need to use services for this matter but i dont know how.

so here is the problem. i have index page which body has only one div and inside the div i have ng-include listening to a function called viewFile() which is described on controllerA. on the first initial attempt i load a view called login.html and display it. when users logs in and its successful, which are handled in controllerB, i return a token and now i want to load main.html page using viewFile() in controllerA. is there a call back function or notify controller or something for this? or can i write a service that takes care of this for me?

I'm not using ngRoute because i dont want my URL to change to mysite.com/#/login.html and then mysite.com/#/main.html

.controlle("A", function ($scope, sharedVariable){
$scope.token = sharedVariable.getToken();
$scope.viewFile = function(){
if($scope.token == "")
   return "view/Login.html";
else
   return "view/main.html";
}
}
.controller("B", function ($scope, $http, sharedVariable)){
  http({
   get ...
   .success: function(data){
      $scope.token = sharedVariable.setToken();
      // INVOKE viewFile from above controller
   }
  })
}

and here is the index.html body part

<body>
    <div ng-controller="A"><ng-include src="viewFile()"></ng-include></div>
</body>
3
  • So are you using full page postbacks then? Commented Jun 18, 2014 at 14:20
  • 1
    It's always better to have part of your code in your question , But , as far as I underestood. You Want that when use log's in , you fire a function , and then show a view ? Am I right ? if so , tell me , I'll give you the right answer Commented Jun 18, 2014 at 14:45
  • yes you got it, i'm trying to post part of the code Commented Jun 18, 2014 at 14:53

2 Answers 2

1

look at this simple example http://jsfiddle.net/derkoe/T85rg/presentation/ here personService.person is shared between two controllers similarly you can write your viewFile function in one service like personService. Then call personService.viewFile from any controller. You can pass $scope as its argumen. Something like below

var myModule = angular.module('myModule', []);
myModule.factory('myService', function($rootScope) {
    var sharedService = {};


    sharedService.viewFile  = function($scope) {
        if($scope.token == "")
            return "view/Login.html";
        else
            return "view/main.html";
        };

    return sharedService;
});
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to change the view using different condition define you viewFile function in some service or put it in routescope. Then you can call it from multiple controllers. But I don't think without refresh angularjs will be able to load a different view html

1 Comment

what i want to achieve here is just like creating a function called viewFile(), and when ever i call it, it change the content of some particular div. its easy in jquery, i'm just new in angularjs and i dont know how to call invoke a function in another controller

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.