1

Problem 1:

I came across this issue where in I need to create objects on the fly based on a loop.

Example:

angular.forEach([0,1,2], function (index) {
    $scope.cc + index = buildMeProto();
});

Am I approaching this the wrong way? Can I create $scope.element based on an index?

Problem 2:

I also notice that in the HTML if you do something like:

<div ng-repeat="black in blacks">
    <lightbox name="black+$index" />
</div>

you can't append an index to an object, in this case 'black' is an object and index is 0, 1,2 etc.

Is there a each way to piggy ride the index to create or invoke elements?

Thanks

4

1 に答える 1

1

問題1

$scopeその場でプロパティを作成したい場合は、[]表記法を使用する必要があります。

angular.forEach([0,1,2], function (index) {
    $scope["cc" + index] = buildMeProto();
});

問題 2 プロパティを追加してオブジェクトを拡張する関数をスコープで呼び出すことができます。

$scope.addIndex = function(person, index){
    person.id = index;
};

jsfiddle の例。

于 2013-05-16T01:30:37.610 に答える