2017-04-12 2 views
0

Angular 1.xでng-repeatを使用していますが、それぞれ固有のID /クラス/ ng-modelを作成しようとしています。ng-repeatの内部でのng-repeatの使用方法

<input type="text" id="url-placeholder" placeholder="Companion URL" class="creative-url-box" ng-model="newCreative.companionUrl"> 

このすべてはng-repeat下にあり、私は新しいオブジェクトを作成したいとき、それは私にすべての時間を同じオブジェクトを作成します。

IDとクラスに{{$id}}を追加しようとしましたが、何かが間違っていました。 どうすればいいですか?

おかげ

答えて

1

私は新しいオブジェクトを作成したいですか、。?あなたは同じ値を取得なぜあなたは同じmodel .Thatsを使用している各反復の間に

ng-model="newCreative.companionUrl"> 

ng-repeatでは、それは各反復において異なる値を有する$indexを提供する。

新しいモーダルインスタンスを作成するために使用できます。 、推奨される方法を

<input type="text" id="url-placeholder" placeholder="Companion URL" class="creative-url-box" ng-model="newCreative.companionUrl{{$index}}"> 

しかし:

<div ng-repeat="oneRec in arrRec"> 
<input type="text" id="url-placeholder" ng-model="newCreative[$index].companionUrl"> 
</div> 

さて、あなたはインデックス.ITを使用して、それはダイナミックのためのあなたのng-repeat内部

+0

素晴らしい、ありがとう! :) –

+0

:D dpnt upvoteを忘れる:P –

0

は試してみてください。この私に同じオブジェクトのすべての時間を作る

<!DOCTYPE html> 
 
<html> 
 
<head> 
 

 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script> 
 
    <script type="text/javascript"> 
 

 

 
    angular.module("app",[]) 
 
    .controller("ctrl",function($scope){ 
 

 
     $scope.urlData = [{ 
 
     id:1, 
 
     url:'google.com' 
 
     },{ 
 
     id:2, 
 
     url:'stackoverflow.com' 
 
     }] 
 

 

 

 
    }); 
 

 
    </script> 
 
</head> 
 
<body ng-app="app" ng-controller="ctrl"> 
 
<div ng-repeat="data in urlData track by $index"> 
 
    {{data.id}}, 
 
    <input type="text" ng-model="data.url"> 
 
</div> 
 
</body> 
 
</html>

0

使用$index、以下のように新しいオブジェクトを作成しますnewCreativeアクセスの配列を持っていますあなたのイテレータnewCreativeのプロパティcompanionUrlを使用しようとすると、それはコレクションの各アイテムに固有のものになります。例えば同様

<Div ng-repeat = "newCreative in newCreativeCollection track by $index"> 
    <input type="text" id="url-placeholder" placeholder="Companion URL" class="creative-url-box" ng-model="newCreative.companionUrl"> 
</Div> 

どこnewCreativeCollection、のようにする必要があります:

$scope.newCreativeCollection = [{ 
     companionUrl : '', 
     //other properties 
     }, 
     .. othet items 
    ] 
関連する問題