如何用范围内的angularjs变量名连接?

时间:2016-08-26 06:03:47

标签: javascript angularjs

我有这段代码:

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);
        }
      });

和变量" i"传入函数调用,我想这样做var index = $scope.droppedObjects+i.indexOf(data);这是怎么回事?

2 个答案:

答案 0 :(得分:2)

你可以尝试

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

答案 1 :(得分:0)

您可以尝试使用此概念吗?我希望能帮助你。

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);