Vineet
Vineet

Reputation: 4655

Get model array values in controller's service

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.

Upvotes: 2

Views: 131

Answers (1)

dfsq
dfsq

Reputation: 193301

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]"

Upvotes: 2

Related Questions