0

I have a simple issue with AngularJS: the properties outside of the array update normally, but the array elements (that are bound to those properties) don't, here is a plunker to show that: http://plnkr.co/edit/gNtGhiTf228G8rP0O2iB?p=preview

var vm = this;

vm.topLine = {
        netWrittenPremiumLastFye: 1000000,
        netWrittenPremiumYtd: 123000
    }

vm.grossColumnChartData = [
        ['Previous YE', vm.topLine.netWrittenPremiumLastFye],
        ['YTD', vm.topLine.netWrittenPremiumYtd]
    ];

We can use $scope.$watch('something', function(oldValue, newValue) {...}) but that's not a good choice in my situation since i'll have to do that many times, repeatedly.

I want to take advantage of angular 2-way binding as explained above.

Any helps or suggestions are appreciated, thanks!

0

2 Answers 2

0

I think the only way you can do this without using $watch is by updating the array directly in the html binding.

{{ grossColumnChartData = [
        ['Previous YE', vm.topLine.netWrittenPremiumLastFye],
        ['YTD', vm.topLine.netWrittenPremiumYtd]
    ] }}
<br/>
grossColumnChartData = {{grossColumnChartData | json}}

See this: http://plnkr.co/edit/uthXcvoWSV2XNRFJzkck?p=preview

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

2 Comments

Oh but i need to model somewhere else in the controller, not just in HTML. Why the array elements with Angular binding don't change? So curious. Anyway thanks for that, at least i have another option :) Very great if we have somehow to update the array elements in the controller.
There is another option -- $watch -- but you said you don't want to use it! :)
0

I have updated my plnkr and i have my answer now: http://plnkr.co/edit/gNtGhiTf228G8rP0O2iB?p=preview

<div ng-repeat="item in vm.grossColumnChartData">
    <label>Input {{$index+1}}:</label>
    <input ng-model="item.data" type="text"/>
</div>

I was confused between data binding and reference.

I used ng-repeat to update the array elements.

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.