2

Using a factory, I want to get information from one page (text fields and a submit button), put it in an array, and read from that array to post it into a different page. Here is a snippet of my code.

app.factory("service", function(){
    var serv = {};
    serv.arr = [];
    serv.add = (title, name, post, tag) => serv.arr.push({
        "title" : title, "name" : name, "post" : post, "tag" : tag
    });
    return serv;
});

app.controller("createCtrl", ["$scope", "service", function($scope, service) 
{
    display = () => service.add($scope.title, $scope.name, $scope.post, 
    $scope.tag);
        console.log(service.arr);
}]);

app.controller("newsCtrl", ["$scope", "service", function($scope, service){
    $scope.newsPage = "News";
    $scope.array = service.arr;
}]);

I know I'm probably way off but at this stage I can't even tell if any information is being added to the array.

3
  • what do you want to acheive here ? can you specify it ? Commented Oct 30, 2018 at 6:31
  • I can try. I have a page that works with a few URL's. Two of those URL's take me to a page called "create" and another called "news". On the "create" page I have a few text input fields in a form, what I want to do is take information typed and submitted in this form, capture it in an array and display the input on the "news" page. Commented Oct 30, 2018 at 6:33
  • you can communicate between controllers via $emit event emitting. Commented Oct 30, 2018 at 6:38

1 Answer 1

2

Try below code for set & get data from factory. Click on SAVE DATA & then GET DATA buttons to see the actions

(function(ng, app){
    app = angular.module('app', [])
    


app.factory("service", function(){  
    
var serv = {};
var arr = [];
    return {
  
      add : function (title, name, post, tag) {
        arr.push({
            "title" : title, "name" : name, "post" : post, "tag" : tag
           });
      },
    
      get : function (firstname) {
        return arr[0];
      }
    
    }
});

    
app.controller("createCtrl", ["$scope", "service", function($scope, service) 
{
    $scope.display = function(){
       service.add($scope.title, $scope.name, $scope.post, $scope.tag); 
    };
}]);

app.controller("newsCtrl", ["$scope", "service", function($scope, service){
    $scope.newsPage = "News";
   
    $scope.getData = function(){
       $scope.array = service.get();
    };        
}]);


}(angular));
input {
 margin: 5px;
}
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
  <body>
    
    <div ng-controller="createCtrl as main">
      <h1>create Ctrl</h1>
    <input ng-model="title" placeholder="title" /><br/>

      <input ng-model="name" placeholder="name" /><br/>

      <input ng-model="post" placeholder="post" /><br/>

      <input ng-model="tag" placeholder="tag" /><br/>
      <button ng-click="display()">  SAVE DATA </button>     
    </div>

<br/> <hr/>

    <div ng-controller="newsCtrl">
      <h2>news Ctrl </h2>
      <button ng-click="getData()">  GET DATA </button>

      <p> title : {{array.title}} </p>
      <p> name : {{array.name}} </p>
      <p> post : {{array.post}} </p>
      <p> tag : {{array.tag}} </p>
      
    </div>
    
  </body>
</html>

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

2 Comments

Ah this is just what I needed! Thank you!
welcome :) :) .. you can accept as answer if it solves your problem.

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.