1

I have a textarea in my HTML like this:

<textarea ng-model="commentBox"></textarea>

To access this i simply use "$scope.commentBox" in my controller. But my question is, how do i access the same commentBox within a different controller?

I do have a factory/service setup for this purpose, but i cant figure out how to get the commentBox value in there, for my other controller to use it.

In my factory i have an object var saved = {} and I want to add a property called "comment", with the value of whatever is inside the textarea. Like this saved.comment = commentbox And then access that value from the other controller.

I'm still new at Angular and tried to send the scope information in a parameter to the facory.

var saved = {};

factory.addComment = function (commentbox) {
  saved.comment = commentbox
}

Then have my controller send the scope information on a button click,

$scope.testFunction = function () {
  myFactory.addComment($scope.commentBox);
} 

But yeah, that did not work out.

Note that i need this to work within the factory, and not by using another ng-controller in the HTML.

3
  • Is there any reason you couldn't do ng-Change=factoryProduct.addComment(commentBox), where the factoryProduct is the instance that the Controller has created from the Factory? I assume when you say you're using Factories you mean something like this sitepoint.com/tidy-angular-controllers-factories-services Commented Nov 2, 2015 at 18:15
  • Not familiar with ng-change, never used it. But i have a route that controlls which controllers to be used on what html. And i have ng-model on page 1, which i need to get the value of on my page 2. And to do so, im trying to put that value from controller 1 into the factory, so controller 2 can access it. Commented Nov 2, 2015 at 18:46
  • I think you just need something like ng-change="vm.addComment(commentBox)", where vm.addComment does something like yourService.setCurrentComment(newValue). Commented Nov 2, 2015 at 18:53

1 Answer 1

1

You need to return your saved var in your factory

var saved = {}; 


factory.addComment = function (commentbox) {
  saved.comment = commentbox
}

return saved

Here there is an example using a service

app.service('fakeService', function() {
  var savedData = {};

  var addComment = function(newComment) {
      savedData.commnet = newComment;
  };

  var getComment = function(){
      return savedData.comment;
  };

  return {
    addComment: addComment,
    getComment: getComment
  };

});

To inject a factory/service in your controller

app.controller('fakeController', ['$scope','yourFactory',function ($scope, yourFactory) {

   console.log(yourFactory.comment) // here you should log saved comment

}])

Then in your controller, you can inject the factory/service and access to saved data. But remember, is your refresh your page, data will be lost, so, to avoid that, you should persist data on your DB.

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

4 Comments

I do return the saved variable and everything else that needs to be returned. I just dont know how to add the scope value into the factory, so that my other controllers can access it.
Thanks for your replies, my factory is already injected in the controllers. I made a simple jsfiddle to narrow down my problem. jsfiddle.net/aw5p5hyr How do MyCtrl2 get the value of commentBox (like in MyCtrl1), using a factory?
Where are you using MyCtrl2 in your fiddle? And where are you defining or using your service?
@qua1ity I think you need add things to your fiddle.

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.