Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
try name="thing[{{$index}}]" –  tomwilde yesterday
    
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? –  bmw0128 yesterday

1 Answer 1

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.

share|improve this answer
    
This did not work –  bmw0128 13 hours ago
    
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? –  tomwilde 1 hour ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.