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 2D array that is iterated through via ng-repeat. This array is actually part of a spreadsheet app I am building, where the initial values in the spreadsheet are that of the array. I wrote a function that would update this array as the user changed input values, however, I've been informed that I need to use the $watch method instead of an onchange method.

I'm really thrown off because I don't understand how I can change the initial array's values through the $watch method. I see that it detects change, but I want it to keep track of that change and actually alter the initial array.

Here is my Javascript, including the beginnings of a $watch method:

sheet= function($scope, $parse){
    $scope.columns = headers;
    $scope.rows = allData.length;
    $scope.cells = {};
    $scope.$watch(
        function() {console.log("Change!");},
    );
    $scope.values = allData.map(function(c,row){
        return c.map(function(data){
            return {
                content: data,
                model: null,
                color: makeColors(data)
            };
        });
    });
};

changeData = function(row, col, data){
    allData[row][col] = data;
};  

My HTML:

<div ng-app ng-controller="sheet">
    <table>
        <tr class="column-label">
            <td ng-repeat="column in columns">{{column}}</td>
        <tr ng-repeat="value in values">
            <td class="row-label" ng-repeat="data in value">
                <div>
                    <input type="text" id="inputValue"  ng-model="data.content" class="{{data.color}}" onchange="changeData({{$parent.$index}},{{$index}},this.value)">
                </div>

            </td>
        </tr>
    </table>
</div>

As you can see, I call the changeData function using the $parent.$index and $index values, which allow me to access and change the initial array. I'm unsure how to do this via $watch.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Your $watch is missing the value it's watching. It won't do anything without that.

But why are you going through such trouble to update your model? Your model is updated automatically when you bind ng-model to an input.

Here is a plunker showing how I would approach it: http://plnkr.co/edit/Oxu1NV?p=preview

share|improve this answer
    
What should I bring up in an alert or print to the console to see my updated array? I have tried allData but it remains unchanged, and sheet.$scope.values are undefined. I guess I mostly just don't understand what is changing and what happens when it's changed. I see it is bound to data.content, but that doesn't do anything that I've noticed. –  user2494584 Jul 9 '13 at 20:54
    
If you notice in my plunker, I output the drinks model in a <pre> block at the bottom of the page. You can see data being updated there if you edit any of the drink ingredients. I usually just output it in the view so I can see changes happening to the model in real time. If a change is happening in the controller, then I use console.log. –  Sharondio Jul 9 '13 at 21:18
    
In your case, your model is returning function calls. It might be simpler to just build a complex JS object with the actual data. –  Sharondio Jul 9 '13 at 21:21
    
Since it's bound with the input, you helped point me in the right direction of having everything working via ng-change; however, how would I access the color or any other field that could be in the values? Can the input be bound to multiple things? –  user2494584 Jul 9 '13 at 22:45
    
I'm not sure what you mean by "access". To output them? –  Sharondio Jul 9 '13 at 23:18

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.