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) })
$loginin the About controller?