0

I have a factory, "itemData", that holds a service.

 app.factory('itemData', function () {

     var itemData = {

         Title: 'I should be different',

         getTitle: function () {
             return itemData.Title;
         },

         setTitle: function (title) {
             return itemData.Title=title;
         },

         // This is the function to call all of the sets for the larger object
         editItem: function (entry)
         {
            itemData.setTitle(entry);
         }


     };

     return itemData;

 });

I have 2 controllers (in different files) associated with 2 separate views. The first:

 // IndexCtrl
 app.controller("IndexCtrl", ['$scope','$window','itemData',

     function($scope, $window, itemData) {
         var entry = "I'm different";

         // After a submit is clicked
         $scope.bttnClicked = function (entry) {

             itemData.editItem(entry);
             console.log(itemData.getTitle();  //<--itemData.Title has changed

             // moves to page with other controller
             $window.location.href = "edit.html";

        };

     }

]);

and the second, which is not doing what I want:

 app.controller("editItemCtrl", ['$scope', '$window', 'itemData', 

     function ($scope, $window, itemData){

         $scope.item = {

            "Title": itemData.getTitle(), //<--- Title is "I should be different"
         } 

 }]);
2
  • 1
    Please pay more attention to whitespace when pasting in code. This was really hard to follow in its original form. Commented Mar 8, 2016 at 22:56
  • 1
    @ConspicuousCompiler Sure thing, I forgot I was posting on a programmer's board haha. Commented Mar 9, 2016 at 2:39

2 Answers 2

3

Do you have some kind of router in place in your Angular app? When you change the location href, is it actually causing full page reload?

Services are held in memory and therefore will not maintain state across page reloads.

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

2 Comments

yes it does refresh the page, this must be the problem. Is there a better way to change pages and not refresh? Perhaps $route?
@user5874570 yes, look into ngRoute or uiRouter
0

I consider that the expression ['$scope', '$window', 'itemData', function ($scope, $window, itemData) force to initialize the dependency injection and for this reason you had two different object.

if you see the doc https://docs.angularjs.org/guide/controller there is a section called Setting up the initial state of a $scope object in this section was described the case of scope but I consider that may be extended at the list of the service in the [...] even in the angular.module(moduleName,[...]) if the [....] are filled the functin return a new module and only for empty square bracket return the current module instance

I hop that this can help you

Comments

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.