Raza Saleem
Raza Saleem

Reputation: 185

how to concat in angularjs variable name with scope?

i have this code:

angular.module('ExampleApp', ['ngDraggable']).
      controller('MainCtrl', function ($scope) {
        $scope.centerAnchor = true;
        $scope.toggleCenterAnchor = function () {$scope.centerAnchor = !$scope.centerAnchor}
        $scope.draggableObjects = [{name:'subject1'}, {name:'subject2'}, {name:'subject3'}];
        $scope.droppedObjects1 = [];
        $scope.droppedObjects2 = [];
        $scope.onDropComplete1=function(data,evt,i){
            var index = $scope.droppedObjects1.indexOf(data);
            if (index == -1)
            $scope.droppedObjects1.push(data);
        }
        $scope.onDragSuccess1=function(data,evt){
            console.log("133","$scope","onDragSuccess1", "", evt);
            var index = $scope.droppedObjects1.indexOf(data);
            if (index > -1) {
                $scope.droppedObjects1.splice(index, 1);
            }
        }
        var inArray = function(array, obj) {
            var index = array.indexOf(obj);
        }
      });

and variable "i" pass in function call and i want to do like this var index = $scope.droppedObjects+i.indexOf(data); how this is possible?

Upvotes: 0

Views: 1125

Answers (2)

VjyV
VjyV

Reputation: 334

Can you try using This Concept. I Hope Help you some portion.

var i=0;
var the_string = 'droppedObjects' + i;

// Get the model //
var model = $parse(the_string);

// Assigns a value to it //
model.assign($scope, 42);

console.log($scope.droppedObjects0);

Upvotes: 0

scokmen
scokmen

Reputation: 571

You can try

var index = $scope["droppedObjects"+i].indexOf(data);

Upvotes: 2

Related Questions