0

i want to call Homecontroller under the LoginController. below is the code of both controller:

//Login controller
    app.controller('LoginController', function ($scope, $cookieStore) {
        $cookieStore.put('email','test@gmail.com');
        // need to call Homecontroller here
    });

// Home controller
app.controller('HomeController', function ($scope, $cookieStore, $location) {


      if ($cookieStore.get('email') != null) {
            $scope.wecomeMessage = $cookieStore.get('email');
        }
        else
        {
            $scope.wecomeMessage = "";
        }
    });
Samir Rawat
  • 53
  • 1
  • 2
  • 8

1 Answers1

0

I think you misunderstood the responsibility of controllers in angular.

How to use controllers from angular docs:

Use controllers to:

  • Set up the initial state of the $scope object.

  • Add behavior to the $scope object.

Do not use controllers to:

  • Manipulate DOM — Controllers should contain only business logic.
  • Putting any presentation logic into Controllers significantly affects its testability.
  • Angular has databinding for most cases and directives to encapsulate manual DOM manipulation.
  • Format input — Use angular form controls instead.
  • Filter output — Use angular filters instead.
  • Share code or state across controllers — Use angular services instead.
  • Manage the life-cycle of other components (for example, to create service instances).

So if you want to create some block of reusable code, you just need to create an angular service, which will include function with needed logic. Then you can inject it to both controllers and they will have necessary functionality.

Example:

app.controller('LoginController', function ($scope, $cookieStore, helperService) {
    $cookieStore.put('email', 'test@gmail.com');

    $scope.wecomeMessage = helperService.getMessage();

});

// Home controller
app.controller('HomeController', function ($scope, $cookieStore, $location, helperService) {

    $scope.wecomeMessage = helperService.getMessage();
 });


app.service('helperService', function ($cookieStore) {

    this.getMessage = function () {
        return $cookieStore.get('email') != null ? $cookieStore.get('email') : "";
    };

});
Kanso Code
  • 7,479
  • 5
  • 34
  • 49
  • ok thanks but in the above program i need to update only `$scope.wecomeMessage ` which is base on the `$cookieStore.get('email')` and it's not depend on any service. the problem is to update welcome message `$scope.wecomeMessage ` – Samir Rawat Sep 05 '16 at 13:03
  • As I understood you want to update `$scope.welcomeMessage` in both controllers depend on what is inside of `$cookieStore.get('email'), Yes? – Kanso Code Sep 05 '16 at 13:26
  • yes correct.... and i follow your above code but the problem is both controller have different page(first one is Login.html and second is partial layout for header Layout.html)..after login it's updating the wecome message but not showing and when i refreshed the page then it shows.... – Samir Rawat Sep 05 '16 at 13:44
  • @SamirRawat, can't solve your problem without plunker, but it seems that you are talking about Home Controller's welcome which shown after refresh. If yes, so it caused because you set `$cookie` in Login controller, and it does not exist in the moment of creation Home controller's message. You can move this line `$cookieStore.put('email', 'test@gmail.com');` to service too, and it could solve you problem. If problem is different, please provide me plunker, so I will be able to help you! – Kanso Code Sep 05 '16 at 13:49
  • @SamirRawat, Home and Login shows NOT FOUND on click, please fix that and I will help you – Kanso Code Sep 05 '16 at 15:11
  • Hi Miki Can you download and open (as a website in Visual studio) because its not working in plunker? – Samir Rawat Sep 05 '16 at 17:49
  • @SamirRawat, here is it [plunker](http://plnkr.co/edit/04FGyTpZd9ShHKD75Ol7?p=preview), but download it, if you need additional explanation, let me know and I will update an answer – Kanso Code Sep 05 '16 at 18:10
  • ok when you open `Index.html` and click on login then it show 1 button then click on it. it will create CookieStore and set value `test@gmail.com` but problem is i need to reload or refresh page for showing `CookieStore` value – Samir Rawat Sep 06 '16 at 09:57
  • @SamirRawat, check my plunker, there is no need to reload anything, just download it and enjoy – Kanso Code Sep 06 '16 at 09:59
  • can you share your plunker url – Samir Rawat Sep 06 '16 at 10:27
  • @SamirRawat, http://plnkr.co/edit/04FGyTpZd9ShHKD75Ol7?p=preview , but download it and run locally, I did the same way as you – Kanso Code Sep 06 '16 at 10:28