0

I have a rather big Json object that I am receiving via $http that I want to share between 2 controllers. To give you an idea - both controllers share the parent most array (marked as levels), and then within the levels there are key:values with nested arrays ( 2 of them) and each controller uses and edits them separately. I am trying to consolidate this as much as possible so I thought if i could control 1 json between 2 controllers this would work.

I want to be able to edits each sub array and push back to the server as a whole, but before I attempt this step I would like to be sure something like this is even possible. I have tried using a factory and service to no avail. I am slo curious if I can do this and I bring in the array - is it 2 way binded? The first controller should be able to add more levels, and the second one can add to them in a differen't way the first one can't. If I were to bring in the json and share it between controllers and added a level on the first one, would it show on the second?

I have been banging my head against this for a while now, so any insight or direction would be very appreciated. Thanks!!

I took a stab at what the json might look like (forgive me if the syntax is off)-

{
"level1": {
 "form": {
    "form1": "sample1",
    "form2": "sample2",
    "form3": 500,
    "form4": 500
}
"sublevels": {
    "subL":  ["1", "2", "3"],
    "help1": "main_window",
    "help2": 500
}},
"level2": {
 "form": {
    "form1": "sample1",
    "form2": "sample2",
    "form3": 500,
    "form4": 500
}
"sublevels": {
    "subL":  ["1", "2", "3"],
    "help1": "main_window",
    "help2": 500
}

}

}

so both controllers would build off the levels, something like "level in levels" ng-repeat type scenario, but controller 1 would use the "Form" key, and controller 2 would use the "subevels" keys. they would be able to edit those sections and post the whole sting back to the server for a save. I don't have much else because the factories/services I Tried to setup didn't seem to work at all. Thanks!

2
  • 2
    Don't describe your code. Post it. Commented Aug 26, 2014 at 14:11
  • I don't have anything working so I've tried to represent the json to make it more clear. I am just wondering if/how something like this would be possible. thanks! Commented Aug 26, 2014 at 14:22

1 Answer 1

1

you can put it on service, but a simpler way is use rootscope, or create a parent controller common for both of your controllers

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.