2

I've been facing an issue since couple of hours. My view template looks like-

<div class="row" ng-repeat="row in CampaignsService.getRows().subItems track by $index">
<div class="col-sm-2">
    <select class="form-control dropDownPercent" ng-model="CampaignsService.dropDownPercent[{{CampaignsService.selectCounter}}]" ng-change="CampaignsService.wow(CampaignsService.dropDownPercent, $index)" ng-options="o as o for o in CampaignsService.showPercentDropDown().values">
    </select>
</div>

<div class="col-sm-2" style="line-height: 32px">
    of visitors send to
</div>

<div class="col-sm-4">
    <select class="form-control" ng-model="campaignSelect" ng-options="campaign.Campaign.id as campaign.Campaign.title for campaign in CampaignsService.getRows().items">
        <option value=""> Please select </option>
    </select>
</div>

<div class="col-sm-4">
    <a class="btn btn-default" target="_blank" href="">Show campaign</a>
</div>

Variable CampaignsService.selectCounter is a counter variable and declared in service but when I'm going to use ng-model="CampaignsService.dropDownPercent[{{CampaignsService.selectCounter}}]" it gives me error -

Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 35 of the expression [CampaignsService.dropDownPercent[{{CampaignsService.selectCounter}}]] starting at [{CampaignsService.selectCounter}}]]

And when I use ng-model="CampaignsService.dropDownPercent['{{CampaignsService.selectCounter}}']" it does not give any error but it takes this variable as string. My question is how could I create a model array and get model's array values in my service ?? I read many questions in stack community and none of the trick work for me. My service under my script, is

.service('CampaignsService', ['$rootScope', 'AjaxRequests', function ($rootScope, AjaxRequests) {
                this.dropDownPercent = [];
                this.selectCounter = 0;

                var gareeb = [];
                this.showPercentDefault = 100;

//                    this.campaignsData = [];
                    this.$rowsData = {
                        items: [], //array of objects
                        current: [], //array of objects
                        subItems: [] //array of objects

                    };


                this.getRows = function () {
                    return this.$rowsData;
                }


                this.addNewRow = function () {
                    var wowRow = {}; //add a new object
                    this.getRows().subItems.push(wowRow);
                    this.selectCounter++;
                    gareeb.push(0);
                }

                this.calculatePercentages = function (index) {
                    angular.forEach(this.getRows().current, function (data, key) {
                        if (key == index) {
                            console.log(data);
                        }
                    })
                }

                this.showPercentDropDown = function ($index) {

                    var balle = 0;
                    var start;

                    angular.forEach(gareeb, function (aha, keywa) {
                        balle += aha;
                    })

                    var last = 100 - balle;

                    var final = [];
                    for (start = 0; start <= last; start += 10) {
                        final.push(start);
                    }

                    return this.values = {
                        values: final,
                    };
                }

                this.wow = function (valueWa, keyWa) {
                    console.log(this.dropDownPercent);
                    gareeb[keyWa] = valueWa;
                    this.changePercentDropDown();
                }


                this.changePercentDropDown = function () {
                    var angElement = angular.element(document.querySelector('.dropDownPercent'));
                    angular.forEach(angElement, function (data, key) {
                        console.log(data);
                    })
                }
            }])

Target model structure should be

ng-model="CampaignsService.dropDownPercent[1]"
ng-model="CampaignsService.dropDownPercent[2]"
ng-model="CampaignsService.dropDownPercent[3]"

A big thanks in advance.

0

1 Answer 1

2

Since you are in context of the Angular expression, you don't need interpolation tags {{...}}. So ngModel directive should look like this:

ng-model="CampaignsService.dropDownPercent[CampaignsService.selectCounter]"
Sign up to request clarification or add additional context in comments.

3 Comments

When I add a new row, CampaignsService.dropDownPercent model's value reflects on all of the dropdowns. Will you please tell me why it is happening ? I am aware with the two/multi way data bindings but now my model is array type, so in the next drop down, value shouldn't be changed.
Because CampaignsService.selectCounter is the same for all rows. You can try ng-model="CampaignsService.dropDownPercent[$index]" instead.
No it is not same for all rows. You can see this.addNewRow function in my service. I am increasing this counter variable value by 1. When I hit this function a new object will be added and counter variable CampaignsService.selectCounter increases by 1.

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.