0

I am a novice with angular so bare with will I try and articulate what I am trying to achieve.

I have two web pages; a main 'login' page and a secondary 'about' page. The about page is nothing more than a page with HTML text. I access both pages simply with a link using ng-href.

I have a service which remembers some user login details which seem to persist when I click to the about page and then back to main.

However, I have no idea how to call a function when the user clicks back to the main page. This function would be the login function within the Main Controllers scope. I would then use the users details in the service to automatically login them in and display the users information.

This thread seems quite similar, but the user seems to be at a more advanced stage than me.

Main.html

<div class="main"> <div class="content" ng-hide="login"> <form> ... login fields ... <button ng-click="$login()">Log in</button> </form> </div> <div class="content" ng-show="login"> <form> ... user info ... </form> </div> <div class="footer"> <a ng-href="#/about"/>About</a> </div> </div> 

About.hml

<div class="about"> <div class="content"> ... plain HTML ... </div> <div class="footer"> <a ng-href='#/main'/>mAIN</a> </div> </div> 

app.js

'

use strict'; angular.module('sguAppApp', [ 'ngResource', 'ngSanitize', 'ngRoute', 'ngAnimate' ]) .constant( 'eURL', '....webpage...' ) .config(function ($routeProvider) { $routeProvider .when('/main', { templateUrl: 'views/main.html', controller: 'MainCtrl' }) .when('/about', { templateUrl: 'views/about.html', controller: 'AboutCtrl' }) .otherwise({ redirectTo: '/main' }); }); 

main.js

angular.module('App') .service('TestService', function() { var TestService = {}; TestService.testVar1 = ''; TestService.testVar2 = ''; TestService.testVar3 = '' return TestService; }) .controller('MainCtrl', function ($scope, $rootScope, .... TestService) { $scope.user = {}; $scope.user.logon = ''; $scope.user.password = ''; $scope.sData = ''; $scope.$login = function () { $scope.loading = true; ... do stuff to testVarNs ... } } .controller('AboutCtrl', function ($rootScope, $scope, TestService) { ... somehow call $login function in MainCtrl using the testVarNs in the service ... (not sure if this is even needed) }) 
4
  • Why do you need to call $login in the About controller? Commented Aug 11, 2014 at 16:45
  • I guess I don't (lack of angular knowledge) but I need to somehow call $login when I return to the main page. I just assumed it had to be done via a controller Commented Aug 11, 2014 at 17:10
  • Are you using angular routing? I don't see that anywhere. Commented Aug 11, 2014 at 17:37
  • @ExplosionPills Yes, I forgot to add that. I have edited the question with the information Commented Aug 12, 2014 at 9:41

1 Answer 1

1

The way you laid things out, either you are in the context of MainCtrl or AboutCtrl, when you navigate from one page to the other, the existing context gets destroyed and the destination one gets instanciated.

The login function should probably live in a service, this way you can call it from wherever you like.

In that way you shold inject your TestService in the controller and call the function in response to a click for example, with ng-click="TestService.$login()"

An alternative approach could be to have another controller that is in a context that wraps your MainCtrl or AboutCtrl, Say an AppCtrl that you define early in your html file, for example:

< body ng-controller="AppCtrl'>...

now, properties defined in the AppCtrl Context are visible inside the child contexts, so if you have a $login function defined there you can call it.

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

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.