0

"myData" in the code below is a json object which I extract from $http.get response (I call the request when my web app is initialised). User has to be able to change the data, so I created the code below with inputs that are filled with values in this object.

<div ng-repeat="(parameter,value) in myData">
    <p>{{parameter}}</p>
    <!-- show only if val is defined (not 0 or null) -->
    <div ng-repeat="(par,val) in value" ng-show="val">
        <p>{{par}} : <input type="text" value={{val}}></p> <!-- val is the value the user can change, and par is  the parameter name such as in { parameter: "value" } -->
    </div>
</div>

I created the button that the user can click when he wants to save the changed data (ng-click="saveData()").

saveData function could then use $http.put(url,data) to update the data somewhere…

What I don't know is how to pass the whole object to the saveData() function ? I know you can pass everything just by adding it like saveData(myVariableOrObject), but I don't see how I would do that in my case.

Probably I am missing something here, since I am fairly new with angularJS. Should my html inputs have some kind of ng-model there that would connect values to controller somehow? I would probably figure this out, but with the whole object, hm ? Would I need to construct the object in a controller function again or is there an easier approach?

If anyone can point me in the right direction I would be glad.

If I was unclear please ask for more info.

Thank you!

1
  • Try onclick="saveData(myData)" Commented Feb 2, 2014 at 19:37

4 Answers 4

1
<input type="text" ng-model="myData[parameter][par]">
Sign up to request clarification or add additional context in comments.

Comments

1

I thing ng-form is good solution for this: http://docs.angularjs.org/guide/forms

2 Comments

hmm.. i think my double ng-repeat wouldn't work well with it? How would i define ng-model values?
You can define it as Matjaz answered <input type="text" ng-model="myData[parameter][par]"> and pass myData object to ng-click function
1

I've written a quick JSFiddle, which you can view here: http://jsfiddle.net/jakemulley/3AK79/1/

As you can see, the 'data' is linked to the ng-model attribute on both inputs, which means you don't need value={{val}}.

Also, which is probably worth noting, is that forms can have the ng-submit attribute, which calls the function inside upon submitting the form (in this case, save(user)).

Note 1: The data provided here can be replaced with your http.get data.

Note 2: By passing through 'user', you get both the username and displayname, which you can post to http.put as the data.

Does that help at all?

1 Comment

thank you, it did help, but unfortunately the exact ng-model answer above helped even more!
0

First of all it's not ng-onclick it's ng-click.

Secondly, {{val}} expression is a one-way binding. Which means changes inside your input tag won't affect val variable. You will need to use ng-model for two-way binding. After that you can easily ng-click="saveData(myData)" or just ng-click="saveData()" and get values inside the controller with $scope.myData .

1 Comment

Yeah, i know, i wrote it correctly in my title, my bad :) thanks for your answer, though Matjaz's answer was directly what I needed

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.