0

Apologies for what is probably a very basic question. I'm making a little demo quiz project with angular. On the first page I have a basic two-way data binding greeting (i.e you enter your name in a box and it welcomes the user e.g <p> Welcome {{name}} </p>

How can I save the name entered and carry this welcome over to the next page/template? Here is the code

<strong>Welcome</strong> {{email}}

  <p class="lead">Please enter your name </p>
      <body ng-app ng-init="email = 'John';">
      <label>enter name<input type="text" ng-model="firstName"/></label><br />
  </body>

And here is my routing

'use strict';


angular
  .module('angularQuizApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .when('/about', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl'
      })
      [...]

I started with a yeoman angular scaffold so have changed very little code. Thank you in advance kind angular wizards

4
  • We have no idea how you handle routing... We can't help you with so few information. Commented Jun 27, 2015 at 11:34
  • Apologies, shall edit :) Commented Jun 27, 2015 at 11:39
  • while using ngcookies , see here : stackoverflow.com/questions/30964795/ng-model-with-cookie/… Commented Jun 27, 2015 at 12:03
  • @Mobeale, I asked it because some developpers handle routing inside the server. In that case, using a service would not have worked (but cookies well) Commented Jun 27, 2015 at 12:56

1 Answer 1

2

To achieve what you wish easily, you could create a service that keep the user name:

angular.module('angularQuizApp').
 factory('StorageService',[
function(){

    var name = "";

    var _setName  = function(name){
        name = name;
    }:

    var _getName = function(){
        return name;
    };  

    return {
        setName : _setName,
        getName : _getName,
    }



}]);

Then in your controllers, call the right methods from service :

angular.module('angularQuizApp').
controller('MyController', ['$scope', 'StorageService',
   function($scope, StorageService) {
     $scope.name = StorageService.getName(); // or setName(name_value)
}]);

By this way, this service keeps the user's name through your angular app. Within this service, you could save whatever you want. But this object/name will be destroyed if your quit the app.

If you want to keep objects persistantly, take a look at angular-storage : https://github.com/grevory/angular-local-storage

EDIT

Here is a functional app that I made : http://plnkr.co/edit/7ZzBYnKmV1xflzi81YQc?p=preview

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

3 Comments

You can also store that kind of important value into the $rootScope.
Unfortunately couldn't get this to work, Dude :( The two way binding worked fine, but it didn't save the value over to the next page
Marius, thanks so much dude! The Plnkr has made it nice and clear

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.