2014-04-30 3 views
0

을 임의의 모델 이름을 생성, 난에 입력 필드NG-반복 내 angular.js 프로젝트에서

<input type="text" ng-model="myColour"> 
<div ng-repeat="c in colors"> 
    <input type="text" ng-model="colour"> 
    <a href ng-click="asd(colour)">Click</a> 
</div> 

때 사용자가 클릭을 포함하는 루프가 입력 필드에, 내가 원하는 옆에 링크를 "클릭" 내 컨트롤러의 해당 입력 필드에 액세스하여 해당 필드 값을 설정하거나 가져옵니다. 내 컨트롤러 코드

$scope.colors = [ 
    {id: 1, name: 'black', shade: 'dark'}, 
    {id: 2, name: 'white', shade: 'light'}, 
    {id: 3, name: 'red', shade: 'dark'}, 
    {id: 4, name: 'blue', shade: 'dark'}, 
    {id: 5, name: 'yellow', shade: 'light'} 
]; 

$scope.asd = function(data){ 
    console.info(data); 
    console.info($scope.myColour); 
    console.info($scope.colour); 
}; 

되어 다음이가보기에 반복되면 내가 "컬러"모델에 액세스 할 수없는 나에게 여기

colour input field data 
my colour input field data 
undefined 

을 제공합니다. 그래서 임의의 모델 이름 (모델에서 색상으로 c.id를 연결)을 만들려고 시도했지만,이 방법을 시도했지만 여러 가지 방법을 시도해 보았습니다.

임의의 ng-model 이름을 생성 할 수있는 방법이 있습니까?

또는

입력 필드의 모델에 액세스 할 수있는 방법이 있나요 "를 클릭"링크를 클릭? 이 같은

답변

1

시도 뭔가 : JS와

<div ng-repeat="c in colors"> 
    <input type="text" ng-model="c.colour"> 
    <a href ng-click="asd(c.colour)">Click</a> 
</div> 

:

// your collection 
$scope.colors = [ 
    {id: 1, name: 'black', shade: 'dark'}, 
    {id: 2, name: 'white', shade: 'light'}, 
    {id: 3, name: 'red', shade: 'dark'}, 
    {id: 4, name: 'blue', shade: 'dark'}, 
    {id: 5, name: 'yellow', shade: 'light'} 
]; 

// add a new key called 'colour' on your colors which will be the model 
angular.forEach($scope.colors, function(value, key){ 
    $scope.colors[key]['colour'] = ""; // match node in html 
});