0

I have a Go struct:

type Foo struct {
    Name   string   `json:"fooName"`
    Things []string `json:"things"`
}

I have an Angular html page:

<input type="text" name="fooName" ng-model="foo.fooName"/>

<div ng-repeat="thing in things">
    <input ng-model="foo.things" type="text" name="thing-{{$index}}"/>
</div>

In the Angular controller I have:

$scope.save= function(){
    Restangular
        .all('foos/new')
        .post($scope.foo).then(function(foo) {
            $location.path('/admin/fooManagement');
        });
};

The rest service call calls:

func CreateFoo(w http.ResponseWriter, r *http.Request) {
    defer r.Body.Close()
    var f Foo
    dec := json.NewDecoder(r.Body)
    dec.Decode(&f)
    log.Println("**** CreateFoo.... ")
    log.Println(&d)
}

Foo's name comes through, but I cannot get the "Things" array populated with the input values. I'm trying to figure out how to get the post service call to populate the array.

2
  • try name="thing[{{$index}}]" Commented Aug 15, 2014 at 20:02
  • That helped, but how do i get multiple inputs working in the repeat, for if they all have the same ng-model value, "foo.things", all inputs will have the same value...i want to have a few inputs that have different values that then populate the array? Commented Aug 15, 2014 at 20:18

1 Answer 1

0

I am not experienced in AngularJS, but I do know Javascript and Go pretty well:

<div ng-repeat="thing in things">
    <input type="text" ng-value="foo.things[$index]" name="thing[{{$index}}]"/>
</div>

Assuming that foo.things and things are not the same array and that both are in scope.

With the ng-value directive, we're binding this input's value to foo.things' element at index $index.

And the name attribute follows Restangular's (undocumented) array-notation.

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

2 Comments

Don't copy-paste the code and say it doesn't work. What exactly doesn't work? What have you tried? What error do you see?
Why did you assume I copied and pasted code, as opposed to adapting your solution to my situation? No explicit error, the array "things" is not being populated with the solution. I did have to use ng-value="foo.things[{{$index}}], but that did not solve the problem. I do not know why it did not work, just as I do not know why my attempt did not work. So, I'm still trying to find the right combo of ng-value and name and such, and I've tried too many combos to list (so I'm hoping for some other variant to try, and I'm hopeful that what you introduced, ng-value, is a step in the right direction:)

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.