0

Problem

I have Controller A, View A and Controller B, View B -

Now I want to load some information from Controller A to View B.

How would I achieve this with AngularJS?

So far I have tried something like this

<div ng-controller="controllerA"> </div>

In my view B but nothing is getting displayed.

1 Answer 1

1

This is a perfect example of when an angular factory could come in handy.

angular.module('yourModuleName')
  .factory('yourFactoryName', function() {

     var info = "";

     return {
         getInfo: function() {
              return info;
          },
         setInfo: function(data) {
              info = data;
          }
     }
}

And then in your controllers you would have some code that looked like this:

angular.module('yourModuleName')
   .controller('yourControllerName', function($scope, yourFactoryName){

       //Makes the factory value available for the controller to use in the view
       $scope.holdFactoryValue = yourFacoryName.getInfo();

       //In another controller, you could have a function like this declared to change the data
       $scope.setFactoryValue = function(data) {
          yourFactoryName.setInfo(data);
       }
   })
Sign up to request clarification or add additional context in comments.

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.